Java Reference
In-Depth Information
Classification of throwable classes
There are hundreds of different kinds of errors or exceptions that could hap-
pen at runtime, ranging from division by 0 to an input-output error to memory
overflow. With so many different possibilities of exceptions, some structure must
be put on them to keep them manageable. Java does this by providing a subclass
structure for them. Figure 10.3 shows a few of the subclasses, with indenting to
denote subclassing. Thus, class Throwable has two direct subclasses: Error and
Exception . If an instance of class Error is thrown, something serious happened
and the program terminates. If an instance of class Exception is thrown, then
either the program catches it and processes it in some way or the program ter-
minates with an error message.
Classes Exception, RuntimeException, and ArithmeticException
To the left in Fig. 10.4 is an object of class Exception . As you can see, the
only components defined in class Exception are two constructors: one with no
parameters and one with one parameter, which, through a super-constructor call,
will be assigned to inherited field detailMessage .
On the right side of Fig. 10.4 is an object of class RuntimeException , which
extends class Exception . It also contains only two components, both construc-
tors. This illustrates the general pattern. There is no need to define anything in a
throwable class besides two constructors. The different subclasses are defined
only to give structure and classification to the large list of exceptions that can
occur during execution of a program.
So that you can see what a throwable class looks like, Fig. 10.5 contains the
definition of class ArithmeticException . It has two simple constructors; that is
all. If you want to write your own throwable class, use this one as a model.
See lesson page
10-2 to obtain a
template for a
subclass of class
RuntimeExcep-
tion
a1
a2
Throwable
Throwable
backtrace
?
backtrace
?
?
detailMessage
?
Throwable() Throwable(String)
getMessage() toString()
getLocalizedMessage()
fillInStackTrace()
Throwable() Throwable(String)
getMessage() toString()
getLocalizedMessage()
fillInStackTrace()
Exception
Exception
Exception()
Exception(String)
Exception()
Exception(String)
RuntimeException
RuntimeException()
RuntimeException(String)
Figure 10.4:
Objects of class Exception and RuntimeException
Search WWH ::




Custom Search