Difference Between Checked and Unchecked Exceptions in Java
In Java, exceptions are mainly divided into two categories:
- Checked Exceptions
- Unchecked Exceptions
Both are used for error handling, but they behave differently during compilation and runtime.
Simple Definition
Checked exceptions are verified at compile time, while unchecked exceptions occur during runtime.
Exception Hierarchy Diagram
Object
|
Throwable
|
+-------> Error
|
+-------> Exception
|
+-------> Checked Exceptions
|
+-------> RuntimeException
|
+-------> Unchecked Exceptions
What are Checked Exceptions?
Checked exceptions are exceptions checked by the compiler during compilation.
Main Characteristics
- Checked at compile time
- Must be handled using try-catch or throws
- Compiler forces handling
- Usually related to external resources
Examples of Checked Exceptions
- IOException
- SQLException
- FileNotFoundException
- ClassNotFoundException
Checked Exception Example
FileReader file =
new FileReader("data.txt");
Compiler Result
Compiler gives error if exception is not handled.
Correct Handling
try {
FileReader file =
new FileReader(
"data.txt"
);
}
catch(
FileNotFoundException e
) {
System.out.println(
"File Not Found"
);
}
Checked Exception Flow
Compiler Detects Risk
|
v
Handling Required
|
v
Program Allowed to Compile
Why Checked Exceptions Exist?
Because some operations are risky and recovery is possible.
Examples
- File may not exist
- Database connection may fail
- Network may be unavailable
What are Unchecked Exceptions?
Unchecked exceptions occur during runtime and are not checked by compiler.
Main Characteristics
- Occur during runtime
- Compiler does not force handling
- Usually caused by programming mistakes
- Extend RuntimeException
Examples of Unchecked Exceptions
- NullPointerException
- ArithmeticException
- ArrayIndexOutOfBoundsException
- ClassCastException
Unchecked Exception Example
String s = null;
System.out.println(
s.length()
);
Runtime Result
NullPointerException
Unchecked Exception Flow
Program Running
|
v
Programming Mistake Happens
|
v
Runtime Exception Thrown
Why Compiler Does Not Force Handling?
Because these exceptions usually indicate bugs in program logic.
Examples
- Accessing null object
- Dividing by zero
- Invalid array index
Main Difference Table
| Feature | Checked Exception | Unchecked Exception |
|---|---|---|
| Checked By | Compiler | JVM During Runtime |
| Handling Mandatory? | Yes | No |
| Occurs During | Compile Time | Runtime |
| Parent Class | Exception | RuntimeException |
| Cause | External Problems | Programming Mistakes |
Checked Exception Internal Flow
Code Compilation
|
v
Compiler Detects Exception
|
v
Developer Must Handle
|
v
Compilation Success
Unchecked Exception Internal Flow
Application Running
|
v
Invalid Logic Executed
|
v
JVM Throws Exception
throws Keyword with Checked Exceptions
void readFile()
throws IOException {
}
Why throws Used?
Responsibility transferred to calling method.
Propagation Flow
Method A Calls Method B
|
v
Checked Exception Occurs
|
v
Exception Propagated
Can Unchecked Exceptions be Handled?
Yes.
Although not mandatory, they can still be handled.
Example
try {
int x = 10 / 0;
}
catch(
ArithmeticException e
) {
System.out.println(
"Handled"
);
}
Why Handle Unchecked Exceptions?
- Prevent crashes
- Improve user experience
- Provide meaningful messages
Difference Between Error and Unchecked Exception
| Feature | Error | Unchecked Exception |
|---|---|---|
| Recoverable? | Usually No | Usually Yes |
| Cause | JVM/System Failure | Programming Mistake |
| Example | OutOfMemoryError | NullPointerException |
Checked Exceptions in Banking Systems
Banking systems use checked exceptions for:
- Database failures
- Payment gateway failures
- File processing errors
- Network communication issues
Banking Example
try {
Connection con =
DriverManager.getConnection(
url
);
}
catch(SQLException e) {
System.out.println(
"Database Failure"
);
}
Banking Flow
Database Request
|
v
Connection Failure
|
v
Checked Exception Thrown
|
v
Recovery Logic Executes
Unchecked Exceptions in Banking Systems
Unchecked exceptions often indicate coding bugs.
Examples
- Null customer object
- Invalid transaction calculation
- Wrong array access
Example
Customer c = null; c.getAccount();
Result
NullPointerException
Checked Exceptions in Spring Boot
Spring Boot commonly uses checked exceptions for:
- Database connectivity
- File uploads
- External API communication
- Messaging systems
Spring Boot Example
public void upload()
throws IOException {
}
Unchecked Exceptions in Spring Boot
Spring Boot uses unchecked exceptions for:
- Validation failures
- Illegal arguments
- Authentication issues
- Programming bugs
Spring Global Exception Flow
REST API Request
|
v
Runtime Exception Occurs
|
v
@ControllerAdvice Handles Error
Checked Exceptions in Microservices
Microservices architectures use checked exceptions for:
- Network failures
- Service communication issues
- Kafka connectivity
- Database operations
Unchecked Exceptions in Microservices
Unchecked exceptions commonly indicate:
- Serialization bugs
- DTO validation failures
- Business logic errors
- Programming mistakes
Microservice Failure Flow
Service Call Happens
|
v
Runtime Failure Occurs
|
v
Fallback or Retry Triggered
Advantages of Checked Exceptions
- Forces proper handling
- Improves reliability
- Prevents silent failures
- Useful for recoverable conditions
Disadvantages of Checked Exceptions
- More boilerplate code
- Can make code complex
- Over-handling may happen
Advantages of Unchecked Exceptions
- Cleaner code
- Less boilerplate
- Suitable for programming errors
- Flexible exception propagation
Disadvantages of Unchecked Exceptions
- Runtime crashes possible
- Bugs may remain hidden
- Harder debugging sometimes
Common Interview Mistake
Many developers think checked exceptions occur during runtime only.
Actually:
- Compiler checks them before execution.
Another Common Mistake
Many developers think unchecked exceptions should never be handled.
Actually:
- They can still be handled if needed.
Best Practices
- Use checked exceptions for recoverable problems
- Use unchecked exceptions for programming bugs
- Avoid catching generic Exception unnecessarily
- Provide meaningful exception messages
- Log exceptions properly
Realtime Enterprise Example
Online Banking Transaction
Transaction Started
|
v
Database Connection Failure
|
v
Checked Exception Raised
|
v
Retry or Rollback Happens
Another Enterprise Example
Payment Processing Bug
Null Payment Object Accessed
|
v
NullPointerException Raised
|
v
Global Exception Handler Triggered
Related Learning Topics
- What is Exception Handling in Java
- What is NullPointerException in Java
- What is throw and throws in Java
- What is Custom Exception in Java
- What is try catch finally in Java
- How JVM Works Internally
- Memory Management in Java
- What is Spring Boot
- What are Microservices
Professional Interview Answer
Checked exceptions and unchecked exceptions are two major categories of exceptions in Java. Checked exceptions are verified by the compiler during compilation and must be either handled using try-catch blocks or declared using the throws keyword. They generally represent recoverable conditions such as file handling issues, database failures, or network communication problems. Unchecked exceptions are subclasses of RuntimeException and occur during runtime due to programming mistakes such as null access, invalid type casting, divide-by-zero operations, or array index violations. The compiler does not force handling of unchecked exceptions because they usually indicate logical bugs in application code. Enterprise applications, Spring Boot systems, banking platforms, distributed microservices, REST APIs, Hibernate ORM frameworks, and cloud-native architectures use checked exceptions for recoverable infrastructure failures and unchecked exceptions for business logic or programming errors. Understanding the difference is critical for building scalable, fault-tolerant, and reliable enterprise systems.
Frequently Asked Questions
What are checked exceptions?
Checked exceptions are compile-time exceptions that must be handled.
What are unchecked exceptions?
Unchecked exceptions occur during runtime and are not mandatory to handle.
Which class is parent of unchecked exceptions?
RuntimeException is the parent class of unchecked exceptions.
Why are checked exceptions important?
They help developers handle recoverable failures properly.
Can unchecked exceptions be handled?
Yes, they can still be handled using try-catch blocks.