Java Exception Hierarchy
In Java, all exceptions and errors are represented as classes that inherit from the Throwable class. Understanding the exception hierarchy is important because it helps developers identify the type of problem that occurred and decide how that problem should be handled.
Overview of the Hierarchy​
The Throwable class is the root of the Java exception hierarchy. It has two major direct subclasses:
ExceptionError
A simplified view of the hierarchy looks like this:
Throwable
├── Error
│ ├── OutOfMemoryError
│ ├── StackOverflowError
│ └── ...
└── Exception
├── RuntimeException
│ ├── ArithmeticException
│ ├── NullPointerException
│ ├── ArrayIndexOutOfBoundsException
│ └── ...
└── Checked Exceptions
├── IOException
│ ├── FileNotFoundException
│ └── ...
├── SQLException
└── ...
This structure shows that both exceptions and errors come from the same root, but Java treats them differently.
1. The Throwable Class​
Throwable is the base class for everything that can be thrown by the JVM or by application code using the throw keyword.
It provides useful methods such as:
getMessage()– returns the message associated with the problemprintStackTrace()– prints the stack trace for debuggingtoString()– returns the name of the exception class along with the message
Example: Using getMessage() and printStackTrace()​
public class ThrowableExample {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error Message: " + e.getMessage());
System.out.println("Stack Trace:");
e.printStackTrace();
}
}
}
Possible Output​
Error Message: / by zero
Stack Trace:
java.lang.ArithmeticException: / by zero
at ThrowableExample.main(ThrowableExample.java:5)
Explanation​
ArithmeticExceptionbelongs to theThrowablehierarchygetMessage()returns the error messageprintStackTrace()helps identify where the exception occurred
2. The Exception Class​
The Exception class represents conditions that applications may want to handle.
It is the main branch used for application-level problems. Most exception handling in Java focuses on objects that come under Exception.
The Exception class is broadly divided into:
- checked exceptions
- unchecked exceptions
Checked Exceptions​
Checked exceptions are checked by the compiler. They must be handled or declared.
Examples:
IOExceptionFileNotFoundExceptionSQLException
Example: Checked Exception​
import java.io.FileReader;
import java.io.FileNotFoundException;
public class CheckedExceptionExample {
public static void main(String[] args) {
try {
FileReader file = new FileReader("nonExistentFile.txt");
} catch (FileNotFoundException e) {
System.out.println("Checked Exception: " + e.getMessage());
}
}
}
Possible Output​
Checked Exception: nonExistentFile.txt (No such file or directory)
Explanation​
FileNotFoundExceptionis a subclass ofIOExceptionIOExceptionis a subclass ofException- because it is checked, Java requires it to be handled or declared
3. The RuntimeException Class​
RuntimeException is a subclass of Exception.
All exceptions that extend RuntimeException are called unchecked exceptions. These are not checked by the compiler.
They usually occur because of:
- invalid logic
- incorrect assumptions
- improper use of objects
- invalid data handling
Common examples include:
ArithmeticExceptionNullPointerExceptionArrayIndexOutOfBoundsExceptionNumberFormatException
Example: Runtime Exception​
public class RuntimeExceptionExample {
public static void main(String[] args) {
String text = null;
System.out.println(text.length());
}
}
Possible Output​
Exception in thread "main" java.lang.NullPointerException
Explanation​
NullPointerExceptionis a subclass ofRuntimeExceptionRuntimeExceptionis a subclass ofException- the compiler does not force the programmer to handle it
This is why RuntimeException is a very important part of the hierarchy.
4. The Error Class​
The Error class represents serious problems that are usually outside the control of the application.
Errors are different from exceptions because they generally indicate failures related to:
- the JVM
- memory
- class loading
- system-level runtime problems
Applications usually do not try to recover from errors in normal code.
Common examples include:
OutOfMemoryErrorStackOverflowErrorVirtualMachineErrorNoClassDefFoundError
Example: StackOverflowError​
public class ErrorExample {
public static void recursiveMethod() {
recursiveMethod();
}
public static void main(String[] args) {
recursiveMethod();
}
}
Possible Output​
Exception in thread "main" java.lang.StackOverflowError
Explanation​
- the method calls itself indefinitely
- the stack keeps growing
- the JVM eventually throws
StackOverflowError
Although errors are part of the Throwable hierarchy, they are not handled the same way as exceptions in normal application code.
Key Relationships in the Hierarchy​
Some important relationships to remember:
Throwableis the root classExceptionandErrorare direct subclasses ofThrowableRuntimeExceptionis a subclass ofException- checked exceptions are subclasses of
Exceptionthat are not subclasses ofRuntimeException
This means:
- every
RuntimeExceptionis anException - every
Exceptionis aThrowable - every
Erroris aThrowable - not every
Exceptionis aRuntimeException
Why Understanding the Hierarchy Matters​
Understanding the hierarchy helps in several ways:
- it helps you catch more specific exceptions
- it prevents overly broad exception handling
- it helps you understand compiler rules for checked exceptions
- it improves debugging by showing how exception types are related
- it builds the foundation for learning
try-catch,throw,throws, and custom exceptions
For example, if you understand that FileNotFoundException is a subclass of IOException, you can decide whether to catch the specific exception or its parent type.
Key Takeaways​
Throwableis the root of the Java exception hierarchyExceptionandErrorare the two major subclasses ofThrowableRuntimeExceptionis a subclass ofException- checked exceptions and unchecked exceptions are both part of the
Exceptionbranch - errors represent serious problems and are usually not handled in normal application code
Summary​
In this chapter, you learned:
- the overall structure of the Java exception hierarchy
- the role of
Throwable - the difference between
ExceptionandError - where
RuntimeExceptionfits in the hierarchy - why hierarchy knowledge is important for effective exception handling