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.
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:
On the next page, we'll look in more detail at Java's unchecked exceptions.
Copyright © Javamex UK 2009. All rights reserved.
|Search this site:|
What do you think of this article? Did it help you? Found a mistake? Feedback and suggestions here