Java Reference
In-Depth Information
turns right around and throws it at main(), this is not considered handling
the exception. Similarly, if method1() does nothing but let the exception
continue on to main(), the exception has not been handled either.
An exception is considered handled by a method if the method stops the
exception from continuing down the method call stack.
Throwable Classes
The three types of exceptions share a common parent: the java.lang.Throwable
class. Only objects of type Throwable can be thrown by the JVM. The Throw-
able class has two child classes: Exception and Error.
The inheritance hierarchy of exceptions is based on the three categories. The
Error class is the parent class of all Java errors; the Exception class is the parent
class of all exceptions, both run time and checked.
Runtime exceptions and checked exceptions are further distinguished by
where they fit in the inheritance hierarchy. If a class is a child of Runtime-
Exception, this child class represents a runtime exception. If a class is a child of
Exception but not a child of RuntimeException, this class is a checked exception.
For example, ArrayIndexOutOfBoundsException and ArithmeticException
are runtime exceptions because they are both child classes of RuntimeException,
whereas IOException and ClassNotFoundException are checked exceptions
because they are child classes of Exception.
Why make the distinction between runtime and checked exceptions when
all the classes are children of Exception? Because the Handle or Declare
Rule (discussed later in this chapter) is an essential Java feature that
applies only to checked exceptions.
Methods of the Throwable Class
Exceptions are Java objects of type Throwable. When you catch an exception,
you catch a reference to a Java object. Each exception class is different and has
its own set of useful methods, but because all exceptions extend from Throw-
able, you can also invoke the methods of the Throwable class on any caught
exception.
The following is a description of some of the methods in Throwable. Be sure
to check the documentation for a complete description of all the methods in
the Throwable class.
Search WWH ::




Custom Search