Java Reference
In-Depth Information
Accordingtotheircause,Javaexceptionscanbeclassifiedintotwo
types:implicitandexplicit.
Implicitexceptions
takeplacewhenthepro-
gramperformsanillegaloperation,forexample,attemptingadivisionby
zerooraccessinganelementarraywhoseindexisoutofrange.Codecan-
notrecoverfromthistypeofexception,althoughtheircausecanoftenbe
avoided.
Explicitexceptions
aregeneratedbytheapplicationbymeans
ofathrowstatementinordertohandlesomespecialcondition.Implicit
exceptionsarecalledruntimeexceptionsintheJavaliteraturewhileex-
plicitexceptionsaresaidtobeuser-defined.Runtimeexceptionsreferto
thefactthatimplicitexceptionsarethrownbytheJavaruntimelibrary.In
reality,allexceptionstakeplaceatruntime.
Java exception classes
Javacontainsseveralclassesthatrelatetoexceptions.Theclasshierarchy
isshownin
Figure19-1
.
Throwable
Error
Exception
...
(User Exceptions)
IOException
RuntimeException
Figure19-1Java'sExceptionClassHierarchy
TheThrowableclassisatthetopoftheexceptionhierarchy.Throw-
ableisextendedbytheclassesnamedErrorandException.Themembers
oftheErrorclassaresystem-levelerrorsthatarethrownbytheJavavir-
tualmachine.Systemlevelerrorsarerare.Althoughitispossibleforap-
plicationstocatchtheseerrors,therecommendedapproachistoletthe
systemhandlethem.Thereislittleanapplicationcandoifthesystem
runsoutofmemoryorencountersanotherterminalcondition.
ThemostimportantpartoftheJavaThrowablehierarchyistheone
thatgoesthroughtheExceptionbranch.Therearecurrently29classes
thatextendException.In
Figure19-1
,
wehaveshownthetwomorenota-
bleones:RuntimeExceptionandIOException.
Table19-1
liststheclasses
that extend RuntimeException and IOException.