4.1 The Throwable Class Hierarchy
← Back to Chapter 4: Exception Handling
All exceptional situations in Java inherit from a single superclass: java.lang.Throwable.
What is Exception Handling?
An exception is an unexpected problem that happens while a program is running.
Exception handling means:
"When something goes wrong, handle the problem properly instead of allowing the application to crash unexpectedly."
Exception Hierarchy
Throwable is the parent of Error and Exception.
┌─────────────────────────┐
│ java.lang.Throwable │
└────────────┬────────────┘
│
┌───────────────────────┴───────────────────────┐
▼ ▼
┌────────────────────────┐ ┌────────────────────────┐
│ java.lang.Error │ │ java.lang.Exception │
│ (Unrecoverable System) │ │ (Application Recovery) │
└───────────┬────────────┘ └───────────┬────────────┘
│ │
┌───────┴────────┐ ┌────────────────┴────────────────┐
▼ ▼ ▼ ▼
OutOfMemoryError StackOverflowError RuntimeException Checked Exceptions
(Unchecked) (Checked at compile time)
• NullPointerException • IOException
• ArithmeticException • SQLException
• IllegalArgumentException • ClassNotFoundException
2. Error vs Exception
| Dimension | java.lang.Error | java.lang.Exception |
|---|---|---|
| Origin | Fatal JVM or hardware-level failures | Application logic, network, or external IO failures |
| Recovery | Unrecoverable: Application should terminate | Recoverable: Application should catch, log, and handle |
| Examples | OutOfMemoryError, StackOverflowError, NoClassDefFoundError | FileNotFoundException, IllegalArgumentException |
| Handling | Never catch Error in production code | Handled using try-catch blocks or propagated via throws |
Never Catch java.lang.Throwable Blindly
Catching Throwable will intercept fatal JVM errors like OutOfMemoryError or ThreadDeath, preventing the JVM from shutting down cleanly or triggering diagnostic dumps.