Exception Hierarchy in Java

Naveen AnamaneniUpdated · 4 min read

Every exception in Java is an object of a class, and those classes form a family tree. Knowing the tree tells you three useful things at a glance: whether the compiler will force you to handle a problem, whether you should try to recover from it, and which catch blocks will catch it. The whole tree grows from one root: Throwable.

The Family Tree

Throwable splits into Error and Exception; RuntimeException is the unchecked branch of Exception

You can ask Java to print each class's ancestors:

Ancestors.java▶ Try it
public class Ancestors {
    static void show(Class<?> c) {
        StringBuilder line = new StringBuilder(c.getSimpleName());
        Class<?> p = c.getSuperclass();
        for (; p != Object.class; p = p.getSuperclass()) {
            line.append(" -> ").append(p.getSimpleName());
        }
        System.out.println(line);
    }
 
    public static void main(String[] args) {
        show(NumberFormatException.class);
        show(NullPointerException.class);
        show(java.io.FileNotFoundException.class);
        show(StackOverflowError.class);
    }
}
Output
NumberFormatException -> IllegalArgumentException -> RuntimeException -> Exception -> Throwable
NullPointerException -> RuntimeException -> Exception -> Throwable
FileNotFoundException -> IOException -> Exception -> Throwable
StackOverflowError -> VirtualMachineError -> Error -> Throwable

How it works

getSuperclass() climbs one level at a time until it reaches Object. Every line ends at Throwable, the only class Java lets you throw and catch.

The Three Branches

BranchMeansExamplesShould you catch it?
ErrorSomething is wrong with the JVM or its environmentOutOfMemoryError, StackOverflowErrorAlmost never; the program usually can't recover
Exception (excluding RuntimeException)Expected problems outside your controlIOException, SQLException, InterruptedExceptionYes; the compiler requires you to handle or declare them (checked)
RuntimeExceptionUsually programming mistakesNullPointerException, ArithmeticException, IllegalArgumentExceptionFix the bug; catch only when you can recover (unchecked)

The next lesson, "Checked vs Unchecked Exceptions", explains why the compiler treats these branches differently.

Why the Tree Matters for catch

A catch block catches its type and every subclass of it:

catch (...)Catches
NumberFormatExceptionOnly NumberFormatException
IllegalArgumentExceptionNumberFormatException too, since it's a subclass
RuntimeExceptionEvery unchecked exception
ExceptionEverything except Errors
ThrowableEverything, including Errors (avoid this)

That's why specific catch blocks must come before general ones (see "Multiple Catch Blocks and Multi-Catch").

Common Mistakes

Catching Throwable or Error. You'd also catch OutOfMemoryError, which you can't fix by catching.

Thinking Error and Exception are the same thing. They're siblings with different purposes.

Catching Exception when you only expect one type. You'll also catch bugs you didn't intend to hide.

Interview Corner

Explain the exception hierarchy in Java.

Throwable is the root. It has two subclasses: Error, for serious problems in the JVM or environment such as OutOfMemoryError and StackOverflowError, which applications normally shouldn't catch; and Exception, for conditions a program can handle. RuntimeException is a subclass of Exception for programming errors such as NullPointerException; it and its subclasses, plus Error, are unchecked. All other exceptions, such as IOException, are checked.

What is the difference between Error and Exception?

Both extend Throwable. An Error signals a serious problem in the runtime environment that an application usually can't recover from, like running out of memory. An Exception signals a condition the application might reasonably catch and handle, like a missing file or invalid input.

Practice

  1. Easy: Use the show method above to print the ancestors of ClassCastException, ArrayIndexOutOfBoundsException and java.sql.SQLException.
  2. Harder: For each, say which branch it's in and whether the compiler forces you to handle it: IOException, IllegalStateException, OutOfMemoryError, InterruptedException.

Quick check: Exception hierarchy

4 questions
1What is the root of all exceptions and errors?
2Which is an Error, not an Exception?
3NumberFormatException is a subclass of:
4What does catch (RuntimeException e) catch?

Key Takeaways

Was this tutorial helpful?

Lesson 215 of 482