100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

Checked vs Unchecked Exceptions in Java

Compare checked and unchecked exceptions in Java, including compiler enforcement, typical use cases, and common exam pitfalls.

Exception HandlingIntermediate8 min readJul 7, 2026
Analogies

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

java
// 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

java
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

text
Checked exception caught: FileNotFoundException
Unchecked exception caught: ArithmeticException

6. 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

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#CheckedVsUncheckedExceptionsInJava#Checked#Unchecked#Exceptions#Syntax#ErrorHandling#StudyNotes#SkillVeris