1. Introduction
Java classifies exceptions into two broad categories: checked exceptions and unchecked exceptions. This distinction determines whether the compiler forces you to handle a given exception, and it reflects an underlying design philosophy about which failures are 'expected' versus which are 'programming bugs'.
Cricket analogy: Java splits failures like a match splits interruptions from errors: a "checked" rain delay is an expected condition the umpire's rulebook forces you to plan for, while an "unchecked" no-ball from overstepping is treated as the bowler's own mistake.
Understanding this distinction is critical for designing robust APIs and for correctly predicting compiler behavior — a frequent topic in Java certification exams and interviews.
Cricket analogy: Knowing this distinction is like a captain knowing exactly which rain-delay protocols are mandatory versus which fielding errors are just bad luck - essential for designing a solid team strategy and a favorite question in coaching certification exams.
2. Syntax
// Checked exception — must be caught or declared
public void readFile(String path) throws IOException {
FileReader fr = new FileReader(path); // may throw IOException
}
// Unchecked exception — no compiler enforcement
public int divide(int a, int b) {
return a / b; // may throw ArithmeticException, not declared
}3. Explanation
Checked exceptions are subclasses of Exception excluding RuntimeException — common examples are IOException and SQLException. They represent recoverable conditions that are typically external to the program's own logic, such as a missing file or a failed database connection. The compiler checks at compile time that checked exceptions are either caught in a try-catch block or declared in the method signature with throws; failing to do either results in a compile error.
Cricket analogy: A checked exception is like a rained-out ground condition (RainDelayException) or a broken stump (SQLException-style equipment fault) - external, recoverable issues the match rulebook forces the umpire to either handle immediately or formally declare before play, or the game can't proceed.
Unchecked exceptions are RuntimeException and its subclasses — common examples are NullPointerException, ArrayIndexOutOfBoundsException, and ArithmeticException. They typically represent programming bugs (dereferencing a null reference, using an invalid index, dividing by zero) rather than conditions the program should routinely expect. The compiler does not require them to be caught or declared, though they can still be caught if desired.
Cricket analogy: An unchecked exception is like a scorer writing down a nonexistent batsman's name (NullPointerException) or announcing a 12th over that doesn't exist (ArrayIndexOutOfBoundsException) - pure human error, not something the rulebook requires you to formally plan for.
Exam trap: Error subclasses (like OutOfMemoryError) are also unchecked in the sense that the compiler doesn't enforce handling them, but they are NOT considered 'unchecked exceptions' in the strict sense, since that term specifically refers to RuntimeException and its subclasses, not Error.
4. Example
import java.io.FileReader;
import java.io.IOException;
public class CheckedUncheckedDemo {
// Checked exception must be declared
static void readFile() throws IOException {
FileReader fr = new FileReader("nonexistent.txt");
}
// Unchecked exception, no throws needed
static int divide(int a, int b) {
return a / b;
}
public static void main(String[] args) {
try {
readFile();
} catch (IOException e) {
System.out.println("Checked exception caught: " + e.getClass().getSimpleName());
}
try {
divide(10, 0);
} catch (ArithmeticException e) {
System.out.println("Unchecked exception caught: " + e.getClass().getSimpleName());
}
}
}5. Output
Checked exception caught: FileNotFoundException
Unchecked exception caught: ArithmeticException6. Key Takeaways
- Checked exceptions are Exception subclasses excluding RuntimeException, e.g. IOException and SQLException.
- Checked exceptions must be caught or declared with throws, enforced by the compiler.
- Unchecked exceptions are RuntimeException and its subclasses, e.g. NullPointerException and ArithmeticException.
- Unchecked exceptions are not checked at compile time and usually indicate programming bugs.
- Checked exceptions represent recoverable, external conditions; unchecked exceptions usually represent logic errors to fix.
Practice what you learned
1. Which of these is a checked exception?
2. What does the compiler require for checked exceptions that unchecked exceptions do not need?
3. Which class do all unchecked exceptions ultimately extend?
4. What typically causes unchecked exceptions?
5. Is OutOfMemoryError considered a checked or unchecked exception?
Was this page helpful?
You May Also Like
Exceptions in Java
Understand what exceptions are in Java, how the Throwable hierarchy is organized, and why exception handling keeps programs robust.
try-catch in Java
Learn how to use try, catch, multi-catch, and try-with-resources blocks in Java to handle exceptions cleanly.
Custom Exceptions in Java
Learn how to define and throw your own checked or unchecked exception classes in Java for clearer, domain-specific error handling.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics