Checked vs Unchecked Exceptions - Interview Questions & Answers
Master Checked vs Unchecked Exceptions with interview-focused questions and answers. Learn compile-time vs runtime exceptions, differences, best practices, and production-ready Java examples.
Checked vs Unchecked Exceptions - Interview Questions & Answers
Introduction
Java exceptions are broadly classified into two categories:
- Checked Exceptions
- Unchecked Exceptions
Understanding the difference is one of the most common Java interview topics.
Choosing the correct exception type improves:
- API Design
- Maintainability
- Error Recovery
- Code Readability
- Production Reliability
Why Interviewers Ask This Topic?
Every enterprise application handles thousands of exceptions every day.
Interviewers expect developers to understand:
- Compile-time checking
- Runtime exceptions
- Exception hierarchy
- Exception propagation
- Best practices
- When to create custom checked or unchecked exceptions
flowchart TD
Throwable --> Exception
Exception --> CheckedExceptions
Exception --> RuntimeException
RuntimeException --> UncheckedExceptions
Interview Question 1
What are Checked Exceptions?
Answer
Checked Exceptions are exceptions that are checked by the compiler.
If a method may throw a checked exception, the compiler requires you to:
- Handle it using
try-catch, or - Declare it using
throws.
Otherwise, the code will not compile.
Common Checked Exceptions
- IOException
- SQLException
- FileNotFoundException
- ClassNotFoundException
- InterruptedException
Diagram
flowchart LR
Method --> CheckedException
CheckedException --> CompilerCheck
CompilerCheck --> HandleOrThrows
Java Example
public void readFile()
throws IOException {
Files.readString(
Path.of("data.txt")
);
}
Production Example
Reading a configuration file during application startup may throw an IOException.
Interview Tip
The compiler forces developers to acknowledge checked exceptions.
Interview Question 2
What are Unchecked Exceptions?
Answer
Unchecked Exceptions are exceptions that occur at runtime.
The compiler does not require developers to handle or declare them.
Unchecked Exceptions extend:
RuntimeException
Common Unchecked Exceptions
- NullPointerException
- ArithmeticException
- IllegalArgumentException
- NumberFormatException
- ClassCastException
- IndexOutOfBoundsException
Diagram
flowchart LR
RuntimeException --> NullPointerException
RuntimeException --> IllegalArgumentException
RuntimeException --> ArithmeticException
Java Example
String name = null;
System.out.println(name.length());
Output
NullPointerException
Production Example
A REST API receives an invalid request and throws an IllegalArgumentException during validation.
Interview Tip
Unchecked Exceptions usually indicate programming errors or invalid input.
Interview Question 3
What is the difference between Checked and Unchecked Exceptions?
Answer
The main difference lies in compile-time checking.
Comparison
| Checked Exception | Unchecked Exception |
|---|---|
| Checked by compiler | Not checked by compiler |
| Must handle or declare | Optional to handle |
| Extends Exception | Extends RuntimeException |
| Recoverable | Usually programming errors |
| Compile-time validation | Runtime validation |
Diagram
flowchart TD
Exception --> Checked
Exception --> RuntimeException
RuntimeException --> Unchecked
Java Example
Checked
throws IOException
Unchecked
throw new IllegalArgumentException();
Interview Tip
A simple rule:
- Checked → External failures
- Unchecked → Programming mistakes
Interview Question 4
Why do Checked Exceptions exist?
Answer
Checked Exceptions force developers to consider recoverable failures.
Typical recoverable situations include:
- File access
- Database access
- Network communication
- External APIs
- Configuration loading
The compiler ensures these cases are handled appropriately.
Diagram
flowchart LR
ExternalSystem --> CheckedException
CheckedException --> RecoveryPossible
Java Example
try {
Files.readString(
Path.of("config.yml")
);
} catch (IOException ex) {
System.out.println(
"Configuration Missing");
}
Production Example
A Spring Boot application attempts to load a configuration file during startup. If the file is missing, the application logs the error and uses default values.
Interview Tip
Checked Exceptions are intended for conditions that applications can reasonably recover from.
Interview Question 5
Why do Unchecked Exceptions exist?
Answer
Unchecked Exceptions represent problems that generally cannot be solved automatically by the application.
These are usually caused by:
- Incorrect code
- Invalid method arguments
- Null values
- Invalid object state
- Programming mistakes
Diagram
flowchart TD
ProgrammingError --> RuntimeException
RuntimeException --> ApplicationFailure
Java Example
public void withdraw(double amount) {
if (amount < 0) {
throw new IllegalArgumentException(
"Amount cannot be negative");
}
}
Production Example
A payment API rejects a negative transaction amount by throwing an IllegalArgumentException.
Interview Tip
Unchecked Exceptions help identify bugs that should be fixed rather than silently recovered.
Interview Question 6
When should you use Checked Exceptions?
Answer
Use Checked Exceptions when the application can reasonably recover from the failure.
Typical scenarios include:
- Reading files
- Database operations
- Network communication
- External API calls
- Configuration loading
These exceptions force developers to handle recovery explicitly.
Diagram
flowchart TD
ExternalOperation --> FailurePossible
FailurePossible --> CheckedException
CheckedException --> RecoverApplication
Java Example
public String loadConfig()
throws IOException {
return Files.readString(
Path.of("config.yml"));
}
Production Example
A banking application loads exchange rates from a remote service.
If the request fails, the application retries or falls back to cached values.
Interview Tip
Choose Checked Exceptions when the caller has a meaningful recovery strategy.
Interview Question 7
When should you use Unchecked Exceptions?
Answer
Use Unchecked Exceptions when the problem is caused by:
- Invalid input
- Programming mistakes
- Illegal object state
- Invalid method arguments
- Business rule violations
These issues generally require code changes rather than runtime recovery.
Diagram
flowchart LR
InvalidInput --> RuntimeException
RuntimeException --> FixApplicationCode
Java Example
public void transfer(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException(
"Invalid amount");
}
}
Production Example
A REST API rejects an invalid account number supplied by the client by throwing an IllegalArgumentException.
Interview Tip
Most Spring Boot applications use RuntimeExceptions for business validation failures.
Interview Question 8
How do Spring Boot applications handle Checked and Unchecked Exceptions?
Answer
Spring Boot usually handles exceptions using centralized exception handling.
Typical approach:
- Throw RuntimeExceptions from the service layer.
- Handle exceptions globally using
@ControllerAdvice. - Return meaningful HTTP responses.
Checked exceptions are generally wrapped inside custom RuntimeExceptions.
Diagram
flowchart TD
Controller --> Service
Service --> Repository
Repository --> Exception
Exception --> GlobalExceptionHandler
GlobalExceptionHandler --> HTTPResponse
Java Example
if (customer == null) {
throw new CustomerNotFoundException(
"Customer not found");
}
Production Example
If a customer ID does not exist, the application returns:
HTTP 404
Customer not found
instead of exposing internal implementation details.
Interview Tip
Centralized exception handling improves consistency across REST APIs.
Interview Question 9
What are the Best Practices for choosing Checked vs Unchecked Exceptions?
Answer
Follow these recommendations.
Use Checked Exceptions for
- File operations
- Database access
- External services
- Recoverable failures
Use Unchecked Exceptions for
- Validation failures
- Illegal arguments
- Invalid object state
- Programming mistakes
Decision Flow
flowchart TD
ExceptionOccurred --> CanCallerRecover
CanCallerRecover --> Yes
CanCallerRecover --> No
Yes --> CheckedException
No --> RuntimeException
Production Example
| Scenario | Recommended Exception |
|---|---|
| Missing Configuration File | Checked |
| Invalid Customer Age | Unchecked |
| Database Timeout | Checked |
| Negative Payment Amount | Unchecked |
| Network Failure | Checked |
| Invalid Request Payload | Unchecked |
Interview Tip
Ask yourself:
Can the caller recover from this problem?
If yes, consider a Checked Exception.
If no, prefer an Unchecked Exception.
Interview Question 10
What are the most common interview questions on Checked vs Unchecked Exceptions?
Answer
Interviewers frequently ask:
- Why are Checked Exceptions required?
- Why does Spring Boot prefer RuntimeExceptions?
- Can we create custom Checked Exceptions?
- When should we convert a Checked Exception into a RuntimeException?
- Why shouldn't we catch generic Exception?
- What is exception wrapping?
- What are the trade-offs between Checked and Unchecked Exceptions?
Diagram
mindmap
root((Interview Topics))
Checked Exceptions
Runtime Exceptions
Exception Wrapping
Recovery
Spring Boot
Global Exception Handler
Custom Exceptions
Interview Tip
Be ready to explain your decision using real production scenarios rather than simply defining each exception type.
Common Interview Mistakes
- Treating all exceptions as RuntimeExceptions.
- Catching generic
Exception. - Swallowing exceptions without logging.
- Using Checked Exceptions for programming bugs.
- Using RuntimeExceptions for recoverable infrastructure failures.
- Exposing internal exception messages to API clients.
- Ignoring exception chaining using constructors that accept a cause.
- Creating deep and unnecessary exception hierarchies.
Quick Revision Cheat Sheet
| Concept | Key Point |
|---|---|
| Checked Exception | Compiler enforces handling |
| Unchecked Exception | Runtime exception |
| Checked | Recoverable failures |
| Unchecked | Programming errors |
| Checked Base Class | Exception |
| Unchecked Base Class | RuntimeException |
| Spring Boot | Usually prefers RuntimeExceptions |
| External Failures | Checked |
| Validation Errors | Unchecked |
| Best Practice | Choose based on recoverability |
Interviewer's Expectations
Junior Java Developer
- Differentiate Checked and Unchecked Exceptions.
- Know common examples.
- Understand compile-time checking.
Senior Java Developer
- Choose the correct exception type.
- Explain recovery strategies.
- Design clean exception hierarchies.
- Build centralized exception handling.
- Apply exception wrapping appropriately.
Solution Architect
- Define enterprise exception handling standards.
- Standardize API error responses.
- Decide when to propagate, wrap, or translate exceptions.
- Ensure secure and observable error handling.
- Balance developer experience, maintainability, and production reliability.
Related Interview Questions
- Exception Basics
- Try-Catch-Finally
- Try-With-Resources
- Custom Exceptions
- Exception Best Practices
- Spring Boot Global Exception Handling
- REST API Error Handling
- Logging Best Practices
- Production Debugging
Summary
Java categorizes exceptions into Checked Exceptions and Unchecked Exceptions, each serving a distinct purpose. Checked Exceptions represent recoverable failures such as file, database, or network issues and require explicit handling or declaration. Unchecked Exceptions represent programming errors or invalid input, allowing applications to fail fast without compiler enforcement.
For interviews, don't simply memorize the definitions. Explain when each type should be used, why recoverability matters, how Spring Boot typically favors RuntimeExceptions for business validation, and how centralized exception handling improves production systems. Supporting your answers with real-world examples such as file processing, payment validation, REST APIs, and database access demonstrates the practical expertise expected from senior Java developers and solution architects.