Exception Hierarchy & Types
Java's exception system is a class hierarchy rooted at Throwable. Understanding the distinction between checked exceptions (must be declared or caught) and unchecked exceptions (runtime bugs) is fundamental to writing robust Java code.
The Exception Hierarchy
- Throwable — root of all errors and exceptions
- Error — JVM-level problems. Do NOT catch: OutOfMemoryError, StackOverflowError, VirtualMachineError
- Exception — application-level problems. Can catch and handle.
- Checked exceptions extend Exception directly (not RuntimeException). Must be caught or declared with throws.
- Unchecked exceptions extend RuntimeException. Not required to catch — indicates programming bugs.
- Common checked: IOException, SQLException, FileNotFoundException, ParseException
- Common unchecked: NullPointerException, IllegalArgumentException, IndexOutOfBoundsException, ClassCastException, ArithmeticException
Checked vs Unchecked in Code
import java.io.*;
import java.util.*;
public class ExceptionTypes {
// Checked exception — caller MUST handle or declare
public static String readFile(String path) throws IOException {
// If you don't declare 'throws IOException', compile error!
BufferedReader reader = new BufferedReader(new FileReader(path));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) sb.append(line).append("\n");
reader.close();
return sb.toString();
}
// Unchecked — no 'throws' needed (but should still be documented)
public static int divide(int a, int b) {
if (b == 0) throw new ArithmeticException("Cannot divide by zero: a=" + a);
return a / b;
}
public static void lookupById(Map<String, String> map, String key) {
Objects.requireNonNull(key, "key must not be null"); // throws NPE with message
String value = map.get(key);
if (value == null) throw new NoSuchElementException("No entry for key: " + key);
}
public static void main(String[] args) {
// Checked: must try/catch or propagate
try {
String content = readFile("catalogue.txt");
} catch (IOException e) {
System.err.println("Failed to read file: " + e.getMessage());
}
// Unchecked: optional to catch (but good to handle at service boundary)
System.out.println(divide(10, 2)); // 5
// divide(10, 0) → ArithmeticException at runtime
}
}Quick Quiz
Tip
Tip
Practice Exception Hierarchy Types in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
JVM enables 'Write Once, Run Anywhere' — bytecode runs on any platform
Practice Task
Note
Practice Task — (1) Write a working example of Exception Hierarchy Types from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Exception Hierarchy Types is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.
Key Takeaways
- Java's exception system is a class hierarchy rooted at Throwable.
- Throwable — root of all errors and exceptions
- Error — JVM-level problems. Do NOT catch: OutOfMemoryError, StackOverflowError, VirtualMachineError
- Exception — application-level problems. Can catch and handle.