Java Reference
In-Depth Information
from being called by passing
false
to
writableStackTrace
. Pass
false
to
writableStackTrace
whenyouplantooverridethedefaultstacktraceandwantto
avoidtheunnecessary
fillInStackTrace()
methodcalls.Similarly,pass
false
to
enableSuppression
whenrepeatedlycatchingandrethrowingthesameexcep-
tionobject(toimplementcontrolflowbetweentwosubsystems,forexample)orinother
exceptional circumstances.
You will notice that
Throwable(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace)
issig-
nifiedasa
protected
constructor.Also,itsJavadocumentationincludesthefollow-
ingsentence:“Subclassesof
Throwable
shoulddocumentanyconditionsunderwhich
suppressionisdisabledanddocumentconditionsunderwhichthestacktraceisnotwrit-
able.”Thisisanexampleof“designanddocumentforclassextension,”whichIdiscuss
Moving down the throwable hierarchy, you encounter the
java.lang.Exception
and
java.lang.Error
classes,whichrespectivelyrep-
resent exceptions and errors. Each class offers five constructors that pass their argu-
mentstotheir
Throwable
counterparts,butprovidesnomethodsapartfromthosethat
are inherited from
Throwable
.
Exception
is itself subclassed by
ror
is itself subclassed by
java.lang.AssertionError
(discussed later in this
chapter),
java.lang.OutOfMemoryError
, and other classes.
Caution
everinstantiate
Throwable
,
Exception
,or
Error
.Theresultingob-
jects are meaningless because they are too generic.
Checked Exceptions Versus Runtime Exceptions
A
checked exception
isanexceptionthatrepresentsaproblemwiththepossibilityofre-
covery,andforwhichthedevelopermustprovideaworkaround.Thecompilerchecks
(examines) the code to ensure that the exception is handled in the method where it is
thrown, or is explicitly identified as being handled elsewhere.
Exception
and all subclasses except for
java.lang.RuntimeException
(and its subclasses) describe checked exceptions. For example, the
CloneNotSup-
portedException
and
IOException
classes describe checked exceptions.