您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 其它文档 > java语言程序设计-基础篇-课件(第13章)英文
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
本文标题:java语言程序设计-基础篇-课件(第13章)英文
链接地址:https://www.777doc.com/doc-2881533 .html