Java Reference
In-Depth Information
Figure 9-2.
A few classes in the exception class hierarchy
The exception class hierarchy starts at the
java.lang.Throwable
class. Recall that the
Object
class is the
superclass for all classes in Java. It is also the superclass of the
Throwable
class. This is the reason that the figure shows
the
Object
class at the top of the class hierarchy. It is to be emphasized that the Java exception class family starts at the
Throwable
class, not at the
Object
class.
When an exception is thrown, it must be an object of the
Throwable
class, or any of its subclasses. The parameter
of the
catch
block must be of type
Throwable
, or one of its subclasses, such as
Exception
,
ArithmeticException
,
IOException
, etc. The following
catch
blocks are not valid
catch
blocks because their parameters are not a
Throwable
or a subclass of
Throwable
:
// A compile-time error. The Object class is not a throwable class.
catch(Object e1) {
}
// A compile-time error. The String class is not a throwable class.
catch(String e1) {
}
The following
catch
blocks are valid because they specify throwable types as a parameter, which are the
Throwable
class or its subclasses:
// Throwable is a valid exception class
catch(Throwable t) {
}