Exception Hierarchy: Throwable, Exception & RuntimeException
1Concept
All exceptions inherit from `java.lang.Throwable`. `Error` represents catastrophic system issues (OutOfMemoryError, StackOverflowError) that should not be caught. `Exception` is divided into: 1. Checked Exceptions (IOException, SQLException) enforced by compiler at build time; 2. Unchecked Exceptions (subclasses of RuntimeException like NullPointerException, IllegalArgumentException) indicating programming bugs.
2Architecture Diagram
[ Throwable ]
/ \
[ Error ] [ Exception ]
(Fatal JVM) / \
[ Checked Exceptions ] [ RuntimeException ]
(IOException, SQL) (NPE, Arithmetic - Unchecked)3Code Example
Core Java
public class ExceptionHierarchyDemo {
public static void checkAccountAge(int age) {
if (age < 18) {
// Unchecked exception indicating caller violation
throw new IllegalArgumentException("Account holder must be at least 18 years old.");
}
}
public static void main(String[] args) {
try {
checkAccountAge(16);
} catch (IllegalArgumentException e) {
System.err.println("Caught Unchecked Exception: " + e.getMessage());
}
}
}4Expected Output
Caught Unchecked Exception: Account holder must be at least 18 years old.
5Key Takeaways
- ✓Checked exceptions must be either caught (`try-catch`) or declared (`throws`).
- ✓Do NOT catch `Throwable` or `Error`; it can mask critical JVM failures.
- ✓Modern enterprise Java favours unchecked RuntimeExceptions for business logic validation.