The Exception Class Hierarchy


When an exception occurs, an exception object is created. When a system-generated exception occurs, the Java runtime will create an instance of the appropriate exception class containing the information pertinent to the exception. You can also define your own exception classes to suit any specialized needs.

The Exception class hierarchy is shown in Figure 12.1. The Throwable class is the superclass ancestor of all exception classes and is a direct subclass of Object . There are two main branches from Throwable . One is headed by the Error class and represents catastrophic exceptions such as stack overflows that are not checked and usually should not be caught. The Exception class is the parent of recoverable exceptions that your program can catch and process. User-defined exception classes are written as subclasses of Exception or of one of the existing Exception subclasses.

Figure 12.1. Exception Class Hierarchy

graphics/12fig01.gif

Runtime exceptions are subclasses of the RuntimeException class and represent exceptions that occur while the program is running. Examples of RuntimeException subclasses include the ArithmeticException and ClassCastException classes. Runtime exceptions can happen anywhere in a program. It would be unreasonable to catch or declare all possible occurrences of a runtime exception, so Java allows them to be uncaught and undeclared. You can catch or declare them if you want to, but it is not required.

Any subclass of Exception that is not a RuntimeException must be either caught or declared by the method that can cause the exception. Examples of this type of exception include the IOException and ClassNotFoundException classes.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net