10 Lesson 4 of 8

Exception Class Hierarchy

Understanding the family tree of all exceptions in Java

Throwable Error Exception Checked Unchecked

1 Why This Topic Now

Imagine taking this topic in week 3 or 4, when you didn't know what a class is, what a superclass is, or what a subclass is. It would have been impossible! But now you are strong students — you know classes, superclasses, subclasses, and inheritance. This is the perfect time.

Exception handling in Java is built entirely on the class hierarchy. Every exception is an object, every exception type is a class, and those classes are organized in a carefully designed inheritance tree. Understanding this tree is the key to writing precise, effective error handling.

2 The Throwable Family Tree

All exceptions in Java descend from a single root: Throwable. This is the ultimate superclass of everything that can be thrown with the throw keyword and caught with catch. Let's see the full family tree:

%%{init: {'theme': 'base', 'themeVariables': {'primaryColor': '#f8fafc', 'primaryTextColor': '#1f2937', 'primaryBorderColor': '#94a3b8', 'lineColor': '#374151'}}}%% classDiagram direction TB class Throwable { <<root>> +getMessage() String +printStackTrace() void } class Error { <<do NOT catch>> Critical system failures } class Exception { <<recoverable>> Problems you CAN handle } class RuntimeException { <<unchecked>> Programming bugs } class ArithmeticException { e.g. division by zero } class NullPointerException { e.g. calling method on null } class IndexOutOfBoundsException { e.g. invalid array index } class ClassCastException { e.g. invalid cast } class IOException { <<checked>> e.g. file read/write failure } class SQLException { <<checked>> e.g. database error } class FileNotFoundException { <<checked>> e.g. missing file } class OutOfMemoryError { JVM ran out of memory } class StackOverflowError { Infinite recursion } Throwable <|-- Error : extends Throwable <|-- Exception : extends Error <|-- OutOfMemoryError : extends Error <|-- StackOverflowError : extends Exception <|-- RuntimeException : extends Exception <|-- IOException : extends Exception <|-- SQLException : extends RuntimeException <|-- ArithmeticException : extends RuntimeException <|-- NullPointerException : extends RuntimeException <|-- IndexOutOfBoundsException : extends RuntimeException <|-- ClassCastException : extends IOException <|-- FileNotFoundException : extends style Throwable fill:#f1f5f9,stroke:#374151,color:#1f2937 style Error fill:#fee2e2,stroke:#991b1b,color:#7f1d1d style OutOfMemoryError fill:#fecaca,stroke:#991b1b,color:#7f1d1d style StackOverflowError fill:#fecaca,stroke:#991b1b,color:#7f1d1d style Exception fill:#fff7ed,stroke:#ea580c,color:#9a3412 style RuntimeException fill:#fee2e2,stroke:#dc2626,color:#991b1b style ArithmeticException fill:#fef2f2,stroke:#ef4444,color:#b91c1c style NullPointerException fill:#fef2f2,stroke:#ef4444,color:#b91c1c style IndexOutOfBoundsException fill:#fef2f2,stroke:#ef4444,color:#b91c1c style ClassCastException fill:#fef2f2,stroke:#ef4444,color:#b91c1c style IOException fill:#dbeafe,stroke:#2563eb,color:#1e40af style SQLException fill:#dbeafe,stroke:#2563eb,color:#1e40af style FileNotFoundException fill:#dbeafe,stroke:#2563eb,color:#1e40af
Diagram Legend

Red = Error branch — do NOT catch these
Orange = Exception — the recoverable parent class
Light Red = RuntimeException and subclasses (e.g., ArithmeticException, NullPointerException)
Blue = Other exceptions (e.g., IOException, SQLException) — we will learn the difference later

3 The Error Branch — Do NOT Catch

Error and Its Subclasses

Error and its subclasses represent serious system failures that are beyond the control of your program. These indicate problems with the JVM itself or the underlying hardware/OS environment. You should never try to catch or recover from these.

Analogy The Earthquake Analogy

We can handle problems inside our car or building. If the engine overheats, pull over. If a pipe bursts, call a plumber. But if there's an earthquake — that's out of our scope. The entire ground beneath us is shaking. There's nothing we can fix about the earth's tectonic plates.

Similarly, if the JVM goes down, everything goes down. Your Java program cannot fix the JVM. An OutOfMemoryError means the JVM has exhausted all available memory — your program can't magically create more RAM. A StackOverflowError means infinite recursion has consumed the entire call stack — there's no stack space left to even run your catch block properly.

That's why Error is on the left branch of our tree, completely separate from Exception. They are fundamentally different beasts.

The Simple Rule

Error = do NOT catch. These are system-level catastrophes.
Exception = CAN handle. These are problems your program can recover from.

4 Summary

Key Takeaways
  • Throwable is the root of the entire exception hierarchy — everything that can be thrown extends it.
  • Error represents catastrophic system failures (like OutOfMemoryError) — do NOT catch these.
  • Exception represents recoverable problems — this is where your try-catch blocks focus.
  • RuntimeException is a special branch under Exception — we will learn the difference between these two branches later when we create our own custom exceptions.

In the next lesson: how exceptions travel through the call stack — exception propagation.

← Lesson 3: Division Problem Lesson 5: Propagation →
Code Block Theme