Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807Chapter13ExceptionHandling1Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807MotivationsWhenaprogramrunsintoaruntimeerror,theprogramterminatesabnormally.Howcanyouhandletheruntimeerrorsothattheprogramcancontinuetorunorterminategracefully?Thisisthesubjectwewillintroduceinthischapter.2Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807ObjectivesTogetanoverviewofexceptionsandexceptionhandling(§13.2).Toexploretheadvantagesofusingexceptionhandling(§13.3).Todistinguishexceptiontypes:Error(fatal)vs.Exception(nonfatal),andcheckedvs.unchecked(§13.4).Todeclareexceptionsinamethodheader(§13.5.1).Tothrowexceptionsinamethod(§13.5.2).Towriteatry-catchblocktohandleexceptions(§13.5.3).Toexplainhowanexceptionispropagated(§13.5.3).Tousethefinallyclauseinatry-catchblock(§13.6).Touseexceptionsonlyforunexpectederrors(§13.7).Torethrowexceptionsinacatchblock(§13.8).Tocreatechainedexceptions(§13.9).Todefinecustomexceptionclasses(§13.10).3Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807Exception-HandlingOverview4QuotientRunQuotientWithIfRunQuotientWithExceptionRunShowruntimeerrorFixitusinganifstatementWhatiftheruntimeerroroccursinacalledmethod?Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807ExceptionAdvantages5QuotientWithMethodRunNowyouseetheadvantagesofusingexceptionhandling.Itenablesamethodtothrowanexceptiontoitscaller.Withoutthiscapability,amethodmusthandletheexceptionorterminatetheprogram.Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807HandlingInputMismatchException6InputMismatchExceptionDemoRunByhandlingInputMismatchException,yourprogramwillcontinuouslyreadaninputuntilitiscorrect.Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807ExceptionTypes7LinkageErrorErrorThrowableClassNotFoundExceptionVirtualMachineErrorIOExceptionExceptionRuntimeExceptionObjectArithmeticExceptionNullPointerExceptionIndexOutOfBoundsExceptionManymoreclassesManymoreclassesManymoreclassesIllegalArgumentExceptionLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807SystemErrors8LinkageErrorErrorThrowableClassNotFoundExceptionVirtualMachineErrorIOExceptionExceptionRuntimeExceptionObjectArithmeticExceptionNullPointerExceptionIndexOutOfBoundsExceptionManymoreclassesManymoreclassesManymoreclassesIllegalArgumentExceptionSystemerrorsarethrownbyJVMandrepresentedintheErrorclass.TheErrorclassdescribesinternalsystemerrors.Sucherrorsrarelyoccur.Ifonedoes,thereislittleyoucandobeyondnotifyingtheuserandtryingtoterminatetheprogramgracefully.Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807Exceptions9LinkageErrorErrorThrowableClassNotFoundExceptionVirtualMachineErrorIOExceptionExceptionRuntimeExceptionObjectArithmeticExceptionNullPointerExceptionIndexOutOfBoundsExceptionManymoreclassesManymoreclassesManymoreclassesIllegalArgumentExceptionExceptiondescribeserrorscausedbyyourprogramandexternalcircumstances.Theseerrorscanbecaughtandhandledbyyourprogram.Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807RuntimeExceptions10LinkageErrorErrorThrowableClassNotFoundExceptionVirtualMachineErrorIOExceptionExceptionRuntimeExceptionObjectArithmeticExceptionNullPointerExceptionIndexOutOfBoundsExceptionManymoreclassesManymoreclassesManymoreclassesIllegalArgumentExceptionRuntimeExceptioniscausedbyprogrammingerrors,suchasbadcasting,accessinganout-of-boundsarray,andnumericerrors.Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807CheckedExceptionsvs.UncheckedExceptions11RuntimeException,Errorandtheirsubclassesareknownasuncheckedexceptions.Allotherexceptionsareknownascheckedexceptions,meaningthatthecompilerforcestheprogrammertocheckanddealwiththeexceptions.Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807UncheckedExceptions12Inmostcases,uncheckedexceptionsreflectprogramminglogicerrorsthatarenotrecoverable.Forexample,aNullPointerExceptionisthrownifyouaccessanobjectthroughareferencevariablebeforeanobjectisassignedtoit;anIndexOutOfBoundsExceptionisthrownifyouaccessanelementinanarrayoutsidetheboundsofthearray.Thesearethelogicerrorsthatshouldbecorrectedintheprogram.Uncheckedexceptionscanoccuranywhereintheprogram.Toavoidcumbersomeoveruseoftry-catchblocks,Javadoesnotmandateyoutowritecodetocatchuncheckedexceptions.Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807UncheckedExceptions13LinkageErrorErrorThrowableClassNotFoundExce