Lesson · Exception Handling
The exception hierarchy: one tree, three roads
15 min readException Hierarchy
Every throwable descends from Throwable, through Error or Exception, with RuntimeException branching off Exception. The road a type lives on decides whether the compiler forces you to handle it and whether you should.
The catch that would have worked
One catch block quietly covers far more than most people expect, and one covers more than it should:
try { riskyWork(); } catch (Exception e) { // NullPointerException, IllegalStateException, IOException: report(e); // all of Exception's descendants land here } // catch (Throwable t) would catch Error too: // OutOfMemoryError, StackOverflowError, NoClassDefFoundError
Predict: which ancestors does NullPointerException have? RuntimeException, then Exception, then Throwable. That chain is the whole design: facts about a type's road (checked, unchecked, or Error) follow from where it sits in this tree.
The tree, branch by branch
Throwable has exactly two children, and Exception has one famous branch of its own:
| Branch | Examples | What it means |
|---|---|---|
Error (from Throwable) | OutOfMemoryError, StackOverflowError, NoClassDefFoundError | The JVM itself hit a wall; applications should not catch these |
Exception direct children (checked) | IOException, SQLException, ClassNotFoundException | Conditions the caller is expected to handle or declare |
RuntimeException (unchecked) | NullPointerException, IllegalArgumentException, IllegalStateException, IndexOutOfBoundsException, ClassCastException, UnsupportedOperationException | Programming errors; the compiler does not force handling |
- `catch (Exception e)` covers both checked and runtime: useful at boundaries, too broad in the middle of logic.
- Errors are usually caught nowhere: they signal conditions (memory, linkage) where proceeding is rarely meaningful; a top-level logger that records them before shutdown is the normal exception.
- Two lookalikes worth separating:
ClassNotFoundException(checked;Class.forName("x")found no such class) versusNoClassDefFoundError(an Error; a class that existed at compile time is missing at runtime). Same symptom, different roads.
The tree is the syllabus. Checked, unchecked and Error are not adjectives someone assigns; they are positions in this hierarchy, and the compiler reads the same map you do.
Catch what you can handle
The road a type lives on predicts the catch policy:
- From the RuntimeException branch: usually fix the cause rather than catch; they mean a bug upstream.
- From the checked branch: catch where the code knows what to do (retry, substitute, report), otherwise declare it and let the caller decide.
- From the Error branch: do not catch, except in a deliberate top-level handler that records and stops.
- Order matters when both branches appear in one try: the more specific catch must come first (the basics lesson digs into that rule).
Recovery, not silence, is the test. A catch block earns its place by doing something the caller cannot: substituting a value, retrying, or translating; everything else belongs one level up.
Hierarchy board · one tree, three roads
pick a typeWhat it is
The code tried to use null as if it were an object.
What to do
Fix where the null came from; do not just catch it.
Every connection in this tree was checked against Java 21.
The hierarchy playbook
Reading any throwable, in order:
- Walk the tree: RuntimeException descendant, checked exception, or Error; the policy follows immediately.
- For runtime types, read the message as a bug report: the class and message name the invariant that broke.
- For checked types, decide the layer that can genuinely act; handle there, declare elsewhere.
- Never catch Throwable or Error to keep going; log at a top-level boundary if you must observe them.
- When creating a type later in this chapter, choose its branch consciously; that choice is the API's tone.
One root, two children, three policies. The whole exception discipline of Java begins with knowing which road a type travels, and the rest of this chapter is learning to drive on each.
Throwable splits into Error and Exception; Exception splits into checked and unchecked
RuntimeException and its family are programming errors the compiler leaves alone; other Exception children are checked and must be handled or declared; Error marks conditions the JVM hit, which applications record at most and otherwise leave alone. Catch Exception at boundaries, be specific inside logic, and never catch Throwable to continue.
Interview angles
Sketch the throwable hierarchy from memory.+
Throwable at the root; Error and Exception as its children; RuntimeException as Exception's most famous child, with the unchecked family around it: NullPointerException, IllegalArgumentException, IllegalStateException and friends.
The design is the policy: checked exceptions sit directly under Exception, runtime ones under RuntimeException, and Errors beside Exception. Knowing the branch tells you whether the compiler forces handling and whether catching is ever appropriate.
Throwable Error -> OutOfMemoryError, StackOverflowError Exception -> IOException, SQLException (checked) RuntimeException -> NullPointerException, IllegalArgumentException
Common misconception: That 'exception' and 'runtime exception' are loose synonyms. They are distinct branches with different compiler rules and different intended uses.
Follow-up: Where does ClassNotFoundException sit, and how is it not NoClassDefFoundError?
Why should you almost never catch Error?+
Errors signal conditions where the JVM or linkage cannot promise sane continuation: memory exhaustion, stack overflow, missing classes at link time. Catch blocks there tend to mask a dying process.
The standard exception is a top-level handler that records the error and exits, or a framework boundary that must report before shutdown. Recovery logic is written for Exceptions, because those are conditions the program can act on.
// top-level only: try { runApplication(); } catch (Throwable t) { log.fatal("unrecoverable", t); throw t; }
Common misconception: That catching Throwable makes a program bulletproof. It mostly converts a loud crash into a quiet corruption.
Follow-up: What specific failure does catching StackOverflowError invite?
ClassNotFoundException versus NoClassDefFoundError: what separates them?+
ClassNotFoundException is a checked exception from an explicit lookup failing (Class.forName, class loaders); NoClassDefFoundError is an Error raised when a class that was present at compile time cannot be initialised or loaded at runtime.
The first is a query with a defined failure mode the caller can handle and report. The second is linkage breaking under the program's feet, often after a static initialiser failure, and it usually repeats on every retry.
Class.forName("com.missing.Driver"); // ClassNotFoundException (checked)
Common misconception: That they are interchangeable spellings. One is a handled lookup miss; the other is a broken link.
Follow-up: Which one can a library document as a normal failure path?
What does `catch (Exception e)` actually cover?+
Every checked exception and every RuntimeException descendant; everything under Exception, both branches combined.
That breadth is why it is welcome at a boundary (a request handler that must not leak failures) and suspicious inside logic, where it catches bugs together with expected failures. Inside, catch the specific types the try can produce.
catch (Exception e) // checked + unchecked: boundary material catch (IOException | ParseException e) // specific: inside logic
Common misconception: That catching Exception also catches Error. It does not; Error is a sibling branch.
Follow-up: What stays uncaught by a catch (Exception) block above it?
How does the hierarchy decide catch ordering?+
A catch clause matches by instanceof, so a broader type above a narrower one makes the narrower clause unreachable; the compiler refuses it with 'exception X has already been caught'.
The fix is ordering: most specific first, broadest last. Multi-catch bundles siblings that are handled identically, but never a type and its own ancestor, which the compiler also refuses (the advanced lesson shows the message).
catch (FileNotFoundException e) { ... } // specific first catch (IOException e) { ... } // broader after
Common misconception: That the JVM searches for the best match like overload resolution. Catch clauses are tested top to bottom; first match wins.
Follow-up: What does the compiler say about a subclass catch clause placed after its superclass?