Sub topic 2
The Exception Family Tree
In the last chapter you saw that an exception is an object Java creates when something goes wrong. Every exception belongs to a class, and those classes are related to each other like a family. Knowing this family tree helps you decide which problems your code should handle and which ones it should leave alone.
The Root: Throwable
A class hierarchy is a set of classes linked by inheritance, where each child class extends a parent class. At the top of the exception hierarchy sits Throwable. Only objects of type Throwable, or one of its children, can be thrown with Java's throw keyword.
Throwable has two main children:
- Error is a serious problem inside the Java Virtual Machine (JVM), the program that runs your code. Running out of memory is one example. Your program usually cannot fix these.
- Exception is a problem your program can often expect and recover from, such as a missing file or bad user input.
Exception has one especially important child, RuntimeException. A RuntimeException usually means there is a bug in the code, such as dividing by zero or using a null reference.
Common Members of Each Branch
| Branch | Example class | When it happens |
|---|---|---|
| Error | `OutOfMemoryError` | The JVM has no memory left |
| Error | `StackOverflowError` | A method calls itself too many times |
| Exception | `IOException` | Reading or writing a file fails |
| RuntimeException | `ArithmeticException` | Integer division by zero |
| RuntimeException | `NullPointerException` | Calling a method on `null` |
| RuntimeException | `NumberFormatException` | `Integer.parseInt("42x")` |
Walking Up the Tree
Every exception "is a" member of each class above it. A NumberFormatException is also an IllegalArgumentException, a RuntimeException, an Exception, and a Throwable. This program prints the full family line:
public class FamilyTree {
public static void main(String[] args) {
Class<?> c = NumberFormatException.class;
while (c != null) {
System.out.println(c.getName());
c = c.getSuperclass();
}
Throwable t = new NumberFormatException("bad number");
System.out.println("Is a RuntimeException? " + (t instanceof RuntimeException));
System.out.println("Is an Exception? " + (t instanceof Exception));
System.out.println("Is an Error? " + (t instanceof Error));
}
}Output:
java.lang.NumberFormatException
java.lang.IllegalArgumentException
java.lang.RuntimeException
java.lang.Exception
java.lang.Throwable
java.lang.Object
Is a RuntimeException? true
Is an Exception? true
Is an Error? falsegetSuperclass() returns the parent class. The loop stops after Object, the parent of every Java class.
Why the Tree Matters
When you catch an exception, you name a class. Java then catches that class and every class below it in the tree. If you catch RuntimeException, you also catch NumberFormatException and NullPointerException. You will use this in the chapters on try and catch.
Exercise
An ArithmeticException is thrown. Which of these would catch it: Exception, Error, RuntimeException, Throwable?
Show answer
Exception, RuntimeException, and Throwable, because ArithmeticException extends RuntimeException, which extends Exception, which extends Throwable. Error would not catch it because Error is on a separate branch.
Exercise
Change the FamilyTree program to start from ArithmeticException.class. What is the first line, and what is the second line of output?
Show answer
The first line is java.lang.ArithmeticException and the second line is java.lang.RuntimeException.