11.5
The Exception Hierarchy
EXCEPTIONS
This example declares an exception class
TestException
. The
main
method
of class
Test
invokes the
thrower
method four times, causing exceptions to be
thrown three of the four times. The
try
statement in method
main
catches each
exception that the
thrower
throws. Whether the invocation of
thrower
completes
normally or abruptly, a message is printed describing what happened.
The declaration of the method
thrower
must have a
throws
clause because
it can throw instances of
TestException
, which is a checked exception class
( 11.2). A compile time error would occur if the
throws
clause were omitted.
Notice that the
finally
clause is executed on every invocation of
thrower
,
whether or not an exception occurs, as shown by the
[thrower(
...
) done]
out
put that occurs for each invocation
11.5 The Exception Hierarchy
The possible exceptions in a Java program are organized in a hierarchy of classes,
rooted at class
Throwable
( 11.5, 20.22), a direct subclass of
Object
. The
classes
Exception
and
Error
are direct subclasses of
Throwable
. The class
RuntimeException
is a direct subclass of
Exception
.
The exception classes declared by the standard packages
java.lang
,
java.util
,
java.io
and
java.net
are called the
standard exception classes
.
Java programs can use the pre existing exception classes in
throw
statements,
or define additional exception classes, as subclasses of
Throwable
or of any of its
subclasses, as appropriate. To take advantage of Java's compile time checking for
exception handlers, it is typical to define most new exception classes as checked
exception classes, specifically as subclasses of
Exception
that are not subclasses
of
RuntimeException
.
11.5.1 The Classes
Exception
and
RuntimeException
The class
Exception
is the superclass of all the exceptions that ordinary pro
grams may wish to recover from.
11.5.1.1
Standard Runtime Exceptions
The class
RuntimeException
is a subclass of class
Exception
. The subclasses
of
RuntimeException
are unchecked exception classes.
Package
java.lang
defines the following standard unchecked runtime excep
tions, which, like all other classes in package
java.lang
, are implicitly imported
and therefore may be referred to by their simple names:
208
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved