11.1 Java Exception Hierarchy: Checked, Unchecked & Error
Key Takeaways
Throwable is the root of Java’s throwable types, extends Object and has the standard subclasses Error and Exception. Code can also define a direct subclass of Throwable.
Checked throwable classes are Throwable and its subclasses outside the RuntimeException and Error branches. A checked exception that can escape a method generally must be declared.
RuntimeException, Error, and their subclasses are unchecked. Their possible propagation does not impose a compiler catch-or-declare requirement.
Error subclasses such as StackOverflowError, OutOfMemoryError, ExceptionInInitializerError, and NoClassDefFoundError signal severe JVM or linkage failures that applications should normally not catch
A custom class that directly extends Exception is checked. One that directly extends RuntimeException is unchecked.
Java Exception Hierarchy: Checked, Unchecked & Error
Independent prep covering the exception-classification objectives of Official Domain 8 (Handling Exceptions). An exception in Java is an object representing an abnormal event that disrupts the normal flow of instructions during execution. Instead of forcing every method to return special integer error codes and cluttering business logic with if checks, Java separates normal control flow from error-handling code and routes failures up the call stack until a matching handler catches them.
Exam questions in this domain test two foundational skills: knowing exact inheritance relationships under java.lang.Throwable, and predicting whether javac enforces the Catch-or-Specify rule (also called Handle-or-Declare) for a given exception type.
The Throwable Class Hierarchy
An exception object has type Throwable or a subclass. A throw statement takes an expression of an appropriate throwable type or the null type. A catch type must be a throwable class. A throws clause can also name a type parameter bounded by Throwable. Attempting to throw or catch a regular class such as String or Object causes a compile-time error because neither inherits from Throwable.
At the top of the exception tree, Throwable extends java.lang.Object. Its two standard direct subclasses are:
java.lang.Error— Represents failures ordinary applications are generally not expected to recover from, including virtual-machine and linkage errors. Java code can also throw an Error directly, such as AssertionError.java.lang.Exception— Represents conditions that an application might reasonably want to anticipate, catch, and recover from.
Under java.lang.Exception, one special subclass—java.lang.RuntimeException—acts as the boundary between checked and unchecked exceptions.
Important
Although Throwable and Exception are the parent types of Error and RuntimeException, the compiler treats Throwable itself and Exception itself as checked exception types. Only RuntimeException (and its subclasses) and Error (and its subclasses) are unchecked.
Comparing the Three Categories
Understanding the exact compiler rules and runtime intent for each branch prevents the most common 1Z0-808 classification traps:
| Category | Class Hierarchy Position | Compiler Check (Catch-or-Specify) | Typical Examples | Should Application Code Catch It? |
|---|---|---|---|---|
| Checked Exception | Throwable and its subclasses except the Error and RuntimeException branches | Checked — must be caught or declared via throws | IOException, FileNotFoundException, SQLException, java.text.ParseException, ClassNotFoundException | Decide where the condition can be handled. A method may propagate a checked exception in its throws clause. |
| Unchecked Exception (Runtime) | RuntimeException and all of its subclasses | Unchecked — optional to catch or declare | NullPointerException, ArithmeticException, IllegalArgumentException, IndexOutOfBoundsException, ClassCastException | Selective — usually indicates a programming bug; fix the logic or validate inputs |
| Error | Error and all of its subclasses | Unchecked — optional to catch or declare | StackOverflowError, OutOfMemoryError, ExceptionInInitializerError, NoClassDefFoundError | Normally No — indicates JVM or environment failure; recovery is rarely safe |
Tracing Checked vs. Unchecked Compilation
Consider how javac reacts when three methods throw three different Throwable subtypes without declaring throws:
import java.io.IOException;
public class HierarchyRules {
public void throwChecked() {
// throw new IOException("disk error"); // DOES NOT COMPILE: unreported exception IOException
}
public void throwRuntime() {
throw new IllegalArgumentException("bad input"); // Compiles: RuntimeException is unchecked
}
public void throwError() {
throw new AssertionError("broken invariant"); // Compiles: Error is unchecked
}
}
Notice that both throwRuntime() and throwError() compile cleanly with no try-catch block and no throws clause. By contrast, uncommenting throw new IOException("disk error"); fails compilation because IOException extends Exception without extending RuntimeException.
What if a method declares throws Throwable or throws Exception? Because both Throwable and Exception are checked types, any caller must either catch them or redeclare them:
public class BroadChecked {
static void risky() throws Throwable {
throw new Throwable("root checked type");
}
public static void main(String[] args) {
// risky(); // DOES NOT COMPILE: unreported exception Throwable
try {
risky();
} catch (Throwable t) {
System.out.println("Caught: " + t.getMessage());
}
}
}
Even though risky() could theoretically throw an unchecked Error or RuntimeException at runtime, its declaration allows a checked Throwable to escape, so the compiler forces main to handle or declare Throwable.
Error Subclasses vs. Application Exceptions
Exam questions often ask whether catching an Error is legal versus whether it is good practice. Those are two separate questions:
- Syntactically legal? Yes. Because
ErrorextendsThrowable, writingtry { ... } catch (Error e) { ... }ortry { ... } catch (Throwable t) { ... }compiles and executes if anErroris thrown on that thread. - Recommended practice? No. Errors such as
OutOfMemoryErrororStackOverflowErrormean the JVM itself has exhausted critical runtime resources or failed to link/initialize a class (NoClassDefFoundError,ExceptionInInitializerError). Attempting to continue execution after catching anErroroften leaves the program in an unstable state.
Warning
Do not confuse NoClassDefFoundError (an unchecked linkage error, for example when a required definition cannot be loaded or the class was already marked erroneous) with ClassNotFoundException (a checked Exception thrown by reflection APIs such as Class.forName("com.example.Missing")).
Creating Custom Exceptions
When designing custom exception classes in Java SE 8, the superclass you choose immediately determines whether callers face a compile-time check:
class InvalidOrderException extends Exception { // Checked exception
public InvalidOrderException(String message) {
super(message);
}
}
class NegativeQuantityException extends RuntimeException { // Unchecked exception
public NegativeQuantityException(String message) {
super(message);
}
public NegativeQuantityException(String message, Throwable cause) {
super(message, cause);
}
}
Throwable exposes standard constructors—no-arg (), (String message), (Throwable cause), and (String message, Throwable cause)—along with getMessage(), printStackTrace(), and getCause(). Calling super(message) passes the detail message up to Throwable so e.getMessage() returns that detail message, which can itself be null.
Advantages of Java's Exception Mechanism
The Java SE 8 Programmer I objectives highlight why exceptions are preferred over legacy status codes:
- Separates error handling from regular logic — Normal code stays inside
trywithout repeatedif (status != OK)branches after every call. - Propagates errors up the call stack automatically — Helper methods do not need to manually forward error codes through five intermediate callers.
- Groups and differentiates error types via inheritance — Catching
IOExceptionhandlesFileNotFoundExceptionand every other I/O subclass uniformly, while specific catch clauses can still handle subtypes individually. - Makes checked outcomes visible at compile time — Ordinary callers must handle or declare checked exceptions. A handler can still discard an exception, so the compiler does not guarantee recovery.
Which class sits directly between java.lang.Object and java.lang.Exception in the Java SE 8 standard library?
java.lang.Error
java.lang.Throwable
java.lang.RuntimeException
java.io.IOException
A method header is declared as public void load() throws Throwable. What happens when a caller invokes load() inside a method that has no try-catch block and no throws clause?
Compilation succeeds because Throwable includes Error and RuntimeException
Compilation succeeds, and any thrown exception becomes unchecked at runtime
Compilation fails only if load() actually throws a checked subclass inside its body
Compilation fails because Throwable itself is a checked type under the Catch-or-Specify rule
Which pair consists strictly of unchecked types that do not require a try-catch block or throws clause to compile?
ExceptionInInitializerError and NumberFormatException
FileNotFoundException and ArithmeticException
ClassNotFoundException and StackOverflowError
java.text.ParseException and NullPointerException
Sections you finish are checked off in the contents.