Home  Exceptions  try/catch  finally  Throwing  Exception hierarchy  Unchecked exceptions  Exception recasting  Uncaught exceptions

The exception hierarchy in Java

We mentioned in our introduction to exceptions that Java exceptions are objects. Since they're objects, different types of exceptions can be subclasses of one another, just as with other objects. In fact, we mentioned that FileNotFoundException is an extension of IOException. If we catch IOException, then by implication, we also catch FileNotFoundException.

From the base upwards, Java exception classes are organised into a hierarchy. There is a basic exception class called Exception as you might expect. But in fact, the base of the hierarchy starts not with Exception but with a class called Throwable, which is then subclassed into Exception and Error. Part of the hierarchy is illustrated in Figure 1.

The rationale behind the hierarchy is as follows:

  • Exception subclasses represent errors that a program can reasonably recover from. Except for RuntimeException and its subclasses (see below), they generally represent errors that a program will expect to occur in the normal course of duty: for example, network connection errors and filing system errors.
  • Error subclasses represent "serious" errors that a program generally shouldn't expect to catch and recover from. These include conditions such as an expected class file being missing, or an OutOfMemoryError.
  • RuntimeException is a further subclass of Exception. RuntimeException and its subclasses are slightly different: they represent exceptions that a program shouldn't generally expect to occur, but could potentially recover from. They represent what are likely to be programming errors rather than errors due to invalid user input or a badly configured environment.


Figure 1: The base of the Java exception hierarchy. Types in red, and their subclasses, are unchecked.

We'll see that in reality, there are exceptions to these uses. For example, there are cases where the organisation of the JDK libraries means that we have to catch an exception that we essentially know will never occur. And there are some cases where we've "got nothing to loose" by trying to catch an Error, even though we may not actually be able to in practice.

Checked vs unchecked exceptions

We mentioned above that certain exceptions are generally indicative of a programming error rather than errors that we'd expect to occur in the normal course of events. (Whereas, it is reasonable for the programmer to expect that a networking or file system error will occur from time to time.) We also mentioned that subclasses of Error are not necessarily programming errors, but still errors that we wouldn't really expect to occur in the normal course of events.

A feature built into the Java language is that Errors and RuntimeExceptions (and their subclasses– marked in red in Figure 1) are what are called unchecked exceptions:

  • unchecked exceptions can be thrown "at any time";
  • methods don't explicitly have to declare that they can throw an unchecked exception;
  • callers don't have to handle them explicitly.

On the next page, we'll look in more detail at Java's unchecked exceptions.

comments powered by Disqus

Unless otherwise stated, the Java programming articles and tutorials on this site are written by Neil Coffey. Suggestions are always welcome if you wish to suggest topics for Java tutorials or programming articles, or if you simply have a programming question that you would like to see answered on this site. Most topics will be considered. But in particular, the site aims to provide tutorials and information on topics that aren't well covered elsewhere, or on Java performance information that is poorly described or understood. Suggestions may be made via the Javamex blog (see the site's front page for details).
Copyright © Javamex UK 2009. All rights reserved.
Search this site:
Threads Database Profiling Regular expressions Random numbers Compression Exceptions C Equivalents in Java

 What do you think of this article? Did it help you? Found a mistake? Feedback and suggestions here