Checked vs Unchecked Exceptions

Naveen AnamaneniUpdated · 5 min read

Some problems are nobody's fault and can happen to perfectly correct code: the file you need was deleted, the database is down, the network is slow. Others mean the code itself is wrong: calling a method on null, dividing by zero, reading past the end of an array. Java treats these two groups differently. For the first group, checked exceptions, the compiler makes sure you've planned for them. For the second, unchecked exceptions, it trusts you to fix the bug instead.

The Compiler Checks One Kind

Checked.java
import java.io.FileReader;
 
public class Checked {
    public static void main(String[] args) {
        FileReader reader = new FileReader("marks.txt");
    }
}
Compiler output
Checked.java:5: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
        FileReader reader = new FileReader("marks.txt");
                            ^
1 error

FileReader's constructor declares that it may throw FileNotFoundException, a checked exception. The program won't even compile until you do one of two things:

Checked exceptions must be caught or declared; unchecked ones aren't checked
CatchOrDeclare.java▶ Try it
import java.io.FileNotFoundException;
import java.io.FileReader;
 
public class CatchOrDeclare {
    static void option1() {
        try {
            new FileReader("marks.txt");
        } catch (FileNotFoundException e) {
            System.out.println("Option 1, caught: " + e.getMessage());
        }
    }
 
    static void option2() throws FileNotFoundException {
        new FileReader("marks.txt");
    }
 
    public static void main(String[] args) {
        option1();
        try {
            option2();
        } catch (FileNotFoundException e) {
            System.out.println("Option 2, caller caught: " + e.getMessage());
        }
        int n = Integer.parseInt("42");
        System.out.println("Unchecked code compiles with no try: " + n);
    }
}
Output
Option 1, caught: marks.txt (No such file or directory)
Option 2, caller caught: marks.txt (No such file or directory)
Unchecked code compiles with no try: 42

How it works

  1. Catch it: option1 handles the problem itself.
  2. Declare it: option2 adds throws FileNotFoundException, passing the responsibility to its caller, main, which then catches it.
  3. Integer.parseInt can throw NumberFormatException, but that's unchecked, so no try is required.

Which Is Which

CheckedUnchecked
ClassesException and its subclasses, except RuntimeExceptionRuntimeException, Error and their subclasses
Checked byThe compilerNobody; they appear at runtime
Usually caused byConditions outside your controlBugs in the code
ExamplesIOException, FileNotFoundException, SQLException, InterruptedExceptionNullPointerException, ArithmeticException, IllegalArgumentException, IndexOutOfBoundsException
What to doPlan for them: catch, retry, or declareFix the code; validate inputs

Why Java Has Both

The idea is simple: if a problem can happen even when your code is correct, the compiler reminds you to plan for it. If a problem only happens because of a bug, forcing a try around every division or every method call would bury the code in noise, so Java leaves those unchecked. You prevent them by writing correct code: checking for null, validating arguments, testing indexes.

Common Mistakes

Adding throws Exception to every method to silence the compiler. It hides which problems can really happen.

Catching an unchecked exception instead of fixing the bug. catch (NullPointerException e) usually means a missing null check.

Thinking "unchecked" means "can't be caught". Any exception can be caught; unchecked only means the compiler doesn't insist.

Interview Corner

What is the difference between checked and unchecked exceptions?

Checked exceptions are subclasses of Exception other than RuntimeException; the compiler enforces the catch-or-declare rule for them, so code that can throw them must either catch them or declare them with throws. They represent recoverable conditions outside the program's control, like IOException. Unchecked exceptions are RuntimeException, Error and their subclasses; the compiler doesn't enforce handling, and they usually indicate programming errors, like NullPointerException.

Is it a good idea to catch NullPointerException?

Generally no. A NullPointerException signals a bug, such as a missing null check or an uninitialized field. Fixing the code is better than catching it; catching it can hide the real problem and leave the program in an inconsistent state.

Practice

  1. Easy: Classify each as checked or unchecked: FileNotFoundException, ClassCastException, SQLException, IllegalStateException, InterruptedException, StackOverflowError.
  2. Harder: Write a method that calls Thread.sleep(500) (which throws the checked InterruptedException). Make it compile once by catching and once by declaring.

Quick check: Checked vs unchecked

4 questions
1Which is a checked exception?
2What does the catch-or-declare rule require for a checked exception?
3Which is unchecked?
4What do unchecked exceptions usually indicate?

Key Takeaways

Was this tutorial helpful?

Lesson 216 of 482