All objects within the Java exception class hierarchy extend from the
Throwable superclass. Only instances of
Throwable (or an inherited subclass) are indirectly thrown by the Java Virtual Machine (JVM), or can be directly thrown via a
throw statement. Additionally, only
Throwables (or an inherited subclass) can be caught via a
Throwable instance contains the current execution stack, captured when the error exception occurred. It can also contain a message (obtained via the
getMessage() method), indicating the relevant error message. Lastly, for exception chains where one error causes another error to be thrown, a
Throwable can obtain a potential cause (collected via the
getCause() method) as well.
In this article we'll dig deeper into the Java exception class hierarchy and see how Java extends beyond the
Throwable superclass to provide the dozens of built-in error and exception classes that may be thrown by the JVM during execution. We'll also update this article in the future as new, detailed exception-specific posts are published, so the full hierarchy list will provide easy navigation between the detailed error-focused entries.
As mentioned, every possible built-in exception class extends directly (or subclasses) from the
Throwable superclass. The full Java exception class hierarchy can be found below:
As with PHP and other prominent languages, the Java exception class hierarchy was built around two distinct categories:
According to the official documentation, an
Error "indicates serious problems that a reasonable application should not try to catch." This category includes things like
ThreadDeath, which should give an idea of the severity of these types of errors. It is generally considered good practice not to explicitly catch
Error classes in code, since they should be dealt with through a change in the application architecture or refactoring, rather than catching (and likely ignoring) them.
On the other hand,
Exceptions indicate "conditions that a reasonable application might want to catch." These are more typical errors that occur from time to time in most applications, particularly during development. Stuff like
IllegalArgumentException are found in the
Exceptions subclass category.
Below we'll briefly discuss each high-level subclass that extends (or inherits from)
Exception. The goal here is just to give a quick overview of these categories, as much more detailed looks into specific exceptions will come in future articles.
AssertionError- Thrown when an assertion has failed.
LinkageError- Thrown when a class dependency has some form of incompatibility, due to changes made after compilation.
ThreadDeath- Thrown when the (now deprecated)
Thread.stop()method is invoked.
VirtualMachineError- Thrown when something goes wrong with the Java Virtual Machine, such as running out of resources.
CloneNotSupportedException- Thrown when attempting to
clonean object of a class that doesn't implement the
InterruptedException- Thrown when a thread is active, but is somehow interrupted in the process.
ReflectiveOperationException- Thrown when attempting to perform an invalid reflection operation, such as loading a class that doesn't exist or calling a method that cannot be found.
RuntimeException- Thrown during normal execution of the application and operation of the Java Virtual Machine. This category includes the most common exceptions such as
That's just a brief overview of the built-in Java exception class hierarchy, but stay tuned for more in-depth articles looking at each of these exceptions in finer detail. Also, be sure to check out Airbrake's powerful Java Error Monitoring software, designed to help you and your team quickly and easily handle all Java exceptions! Try Airbrake free for 30 days.