1. The Hook (The "Byte-Sized" Intro)
In a Nutshell: All exceptions in Java inherit from Throwable, which splits into Error (serious system issues) and Exception (recoverable conditions). Exceptions further divide into Checked (compile-time enforced) and Unchecked (runtime exceptions). Understanding this hierarchy is crucial for proper error handling.
Think of medical emergencies:
- Error (critical): Heart attack (system failure—beyond your control)
- Checked Exception (planned): Surgery (you prepare in advance)
- Unchecked Exception (unexpected): Slip and fall (runtime accident)
2. Conceptual Clarity (The "Simple" Tier)
💡 The Analogy: The Emergency Response System
Throwable: Any emergencyError: Natural disasters (can't control)Exception: Incidents you can handle- Checked: Fire drill (must have evacuation plan)
- Unchecked: Someone trips (unexpected)
Hand-Drawn Logic Map
3. Technical Mastery (The "Deep Dive")
Complete Hierarchy
| Category | Class | Must Handle? | Example |
|---|---|---|---|
| Error | Extends Error | ❌ (shouldn't) | OutOfMemoryError, StackOverflowError |
| Checked | Extends Exception | ✅ (compile-time) | IOException, SQLException |
| Unchecked | Extends RuntimeException | ❌ (optional) | NullPointerException, ArithmeticException |
The "Why" Paragraph
Why two types? Checked exceptions force you to handle predictable errors (file not found, network down). Unchecked exceptions represent programming bugs (null dereference, invalid array index) that SHOULD be fixed in code, not caught. Errors represent catastrophic failures where recovery is impossible—don't catch these!
4. Interactive & Applied Code
import java.io.*;
public class HierarchyDemo {
public static void main(String[] args) {
// 1. CHECKED EXCEPTION (must handle or declare)
try {
FileReader file = new FileReader("data.txt");
} catch (FileNotFoundException e) { // Checked exception
System.out.println("❌ Checked: " + e.getMessage());
}
// 2. UNCHECKED EXCEPTION (optional to handle)
try {
int result = 10 / 0;
} catch (ArithmeticException e) { // Unchecked (RuntimeException)
System.out.println("❌ Unchecked: " + e.getMessage());
}
// 3. ERROR (don't catch in normal code!)
// StackOverflowError would occur here:
// recursiveMethod(); // Infinite recursion
// Demonstrating the hierarchy
demonstrateHierarchy();
}
static void demonstrateHierarchy() {
Exception e1 = new IOException("IO error");
Exception e2 = new NullPointerException("Null");
System.out.println("IOException instanceof Exception: " + (e1 instanceof Exception));
System.out.println("NullPointerException instanceof RuntimeException: "
+ (e2 instanceof RuntimeException));
}
}5. The Comparison & Decision Layer
Decision Tree: Which Exception Type?
6. The "Interview Corner" (The Edge)
The "Killer" Interview Question: "What's the difference between Checked and Unchecked exceptions?" Answer:
- Checked: Compile-time verified. Must handle (try-catch) or declare (throws). Examples:
IOException,SQLException - Unchecked: Extend
RuntimeException. Not compile-time checked. Represent programming errors. Examples:NullPointerException,ArithmeticException
Key: Checked = external factors; Unchecked = internal bugs
Pro-Tip: Favor unchecked exceptions for modern APIs:
// OLD (Java 1.0): Checked everywhere
public void process() throws MyCheckedException { }
// MODERN: Unchecked for flexibility
public void process() {
throw new MyRuntimeException("error");
}Checked exceptions pollute method signatures and break lambdas/streams!