01 / 22

Checked vs.
Unchecked Exceptions

Learn how Java divides exceptions into two categories: those the compiler enforces and those that surface only at runtime.

Java

What Is an Exception?

An exception is an event that disrupts the normal flow of a program. When something goes wrong, Java creates an exception object that contains information about the error: its type, a message, and where it occurred.

Compile-time
Errors caught before your program runs (syntax errors, type mismatches).
Runtime
Errors that occur while your program is executing: exceptions live here.

The Throwable Hierarchy

Throwable Error Exception RuntimeException Checked Exceptions NullPointerException IOException JVM-level issues (don't catch these) Checked (compiler enforced) Unchecked (runtime only)

Two Categories of Exceptions

Checked Exceptions
The compiler requires you to handle them. They represent predictable problems outside your control: missing files, broken network connections, bad user input from external sources.
Unchecked Exceptions
The compiler does not require you to handle them. They represent programming mistakes: null references, bad array indices, illegal arguments. They extend RuntimeException.

The Compiler Rule

For checked exceptions, the compiler enforces a strict contract: every method that might throw a checked exception must either catch it or declare it in its signature.

Method throws checked try-catch block throws declaration Option 1 Option 2

The Formal Classification Rule

1
Checked exception: any class that extends Exception but does not extend RuntimeException.
2
Unchecked exception: any class that extends RuntimeException (which itself extends Exception).
3
Error: also unchecked, but represents serious JVM-level failures (e.g., OutOfMemoryError). You almost never catch these.

Common Checked Exceptions

IOException
Reading or writing a file fails: file not found, disk full, permission denied.
SQLException
A database operation fails: bad query, lost connection, invalid table name.
ClassNotFoundException
Java cannot find a class at runtime that your code references by name.

Common Unchecked Exceptions

NullPointerException
Calling a method on a null reference. Fix: check for null before using an object.
ArrayIndexOutOfBoundsException
Accessing an array index that does not exist. Fix: validate indices before access.
ArithmeticException
Dividing an integer by zero. Fix: check the divisor before dividing.

Predictable vs. Preventable

Checked Unchecked
Extends Exception RuntimeException
Compiler enforced? Yes No
When it happens External failures you cannot prevent Bugs you can prevent
Must catch or declare? Yes No
Typical examples IOException, SQLException NullPointerException, ArithmeticException

Handling with try-catch

try { FileReader reader = new FileReader("data.txt"); // read file contents } catch (IOException e) { System.out.println("File not found: " + e.getMessage()); }

The try block contains code that might throw. The catch block handles the exception if it occurs. This is one of the two ways to satisfy the compiler for checked exceptions.

Declaring with throws

public void readFile() throws IOException { FileReader reader = new FileReader("data.txt"); // read file contents }

Instead of catching the exception yourself, you can declare it in the method signature with the throws keyword. This pushes the responsibility to handle it to whoever calls your method.

How to Decide: Step by Step

1
Identify the exception class. Look at the API documentation or compiler error to find the exact exception name.
2
Check the hierarchy. Does it extend RuntimeException? If yes, it is unchecked. If it extends Exception directly, it is checked.
3
If checked: you must either wrap it in a try-catch or add a throws declaration. The compiler will remind you.
4
If unchecked: no compiler requirement, but consider whether a defensive check (null guard, bounds check) would prevent it entirely.

Example 1: Reading a File

public static void main(String[] args) { try { BufferedReader br = new BufferedReader( new FileReader("grades.csv") ); String line = br.readLine(); System.out.println(line); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } }

Example 2: Null Reference

String name = null; System.out.println(name.length()); // NullPointerException!

This compiles perfectly. The compiler has no complaint because NullPointerException is unchecked. The crash happens only at runtime.

// The correct fix: defensive coding, not try-catch if (name != null) { System.out.println(name.length()); } else { System.out.println("Name is not set"); }

Example 3: Multiple Exceptions

public void processData(String filename, int index) throws IOException { String[] lines = readAllLines(filename); // IOException: checked, declared in throws System.out.println(lines[index]); // ArrayIndexOutOfBoundsException: unchecked, no declaration needed }

Common Mistakes

✕ Catching RuntimeException
Wrapping code in try-catch for NullPointerException instead of adding a null check. Fix the bug, don't hide it.
✕ Empty catch blocks
Writing catch (Exception e) {} silently swallows errors. Always log or handle the exception meaningfully.
✕ Confusing Error with Exception
Trying to catch OutOfMemoryError or StackOverflowError. Errors are not exceptions: they signal unrecoverable JVM failures.

Summary Comparison

Feature Checked Unchecked
Parent class Exception RuntimeException
Compile-time check ✓ Required ✕ Not required
Cause External conditions Programming errors
Handle or declare? Must do one Optional
Best practice try-catch or throws Prevent with code logic
Examples IOException, SQLException NullPointerException, ArithmeticException

Question 1 of 5

Which of the following is a checked exception in Java?

Question 2 of 5

What happens if you write code that could throw a checked exception but do not catch or declare it?

Question 3 of 5

A NullPointerException is unchecked because it:

Question 4 of 5

Which statement about this method is correct?

Question 5 of 5

Which is the best practice for handling an unchecked exception like ArrayIndexOutOfBoundsException?