7.2. Types of Exceptions

An exception is always an object of some subclass of the standard class Throwable. This is true for exceptions that you define and throw yourself, as well as the standard exceptions that arise due to errors in your code. It's also true for exceptions that are thrown by methods in one or another of the standard packages.

Two direct subclasses of the class Throwable—the class Error and the class Exception—cover all the standard exceptions. Both these classes themselves have subclasses that identify specific exception conditions. Figure 7-1 shows the hierarchy to which these classes belong.

Figure 7.1. Figure 7-1

7.2.1. Error Exceptions

The exceptions that are defined by the Error class and its subclasses are characterized by the fact that they all represent conditions that you aren't expected to do anything about, so you aren't expected to catch them. Error has three direct subclasses—ThreadDeath, LinkageError, and VirtualMachineError:

  • The first of these sounds the most serious, but in fact it isn't. A ThreadDeath exception is thrown whenever an executing thread is deliberately stopped, and for the thread to be destroyed properly, you should not catch this exception. In some circumstances you might want to catch it—for clean-up operations, for example—in which case you must be sure to rethrow the exception to allow the thread to die peacefully. When a ...

Get Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.