资源描述
C+ProgrammingCHAPTER13EXCEPTIONHANDLING113.1ExceptionHandling13.2ExceptionMatching13.3ConstructorsandDestructorsinExceptionHandling213.1ExceptionHandlingAnexceptionisarun-timeerrorcausedbysomeabnormalcondition.InC+,afunctionfcanrecognizeconditionsthatidentifyexceptionsandthensignalthatanexceptionhasoccurred.Thissignalingprocessiscalledthrowinganexception.313.1ExceptionHandlingOncethrown,anexceptioncanbecaughtorhandledbyafunctionthatinvokesfbyusingacatchblock.Acatchblockisanexceptionhandlerthatoccursafteratryblock,whichisusedtoindicateinterestinexceptions.413.1ExceptionHandlingf()catchingandhandlingerrorfunctionerrorinvokedspreadingWhyh()doestg()rectifytheerror?int*p=newint1000h()causeandthrowanerror513.1ExceptionHandlingTheperfectobjectivewastomapexceptionstosomeotherformoferrorpropagationshouldadesignerchoosetodoso.Notthatitwasalwaysbesttodoso,butthatitcouldbedone.613.1ExceptionHandlingthrowingexceptioncatchingandhandlingexceptiontry.throw.expressionexpression;catch(exceptiontype)expressioncatch(exceptiontype)expression.713.1ExceptionHandlingThekeywordthrowcreatesanobjectthatisntthereundernormalprogramexecution.Thentheobjectis,ineffect,“returned”fromthefunction,eventhoughthatobjecttypeisntnormallywhatthefunctionisdesignedtoreturn.Eachcatchclause(exceptionhandler)islikealittlefunctionthattakesasingleargumentofoneparticulartype.813.1ExceptionHandlingRemarks:A)Exceptionismadeandthrownbythrowblock.B)Thecodesegmentthatcouldcauseanexceptionwillbeinthetryblock.C)Thecatchblockwillnotinvokedifthereisnotanyexceptionintryblock,andthecodenexttothelastcatchblockwillbeinvoked.913.1ExceptionHandlingD)Ifanexceptionisthrown,type-matchedcatchblockwillcatchandhandleit.E)Ifnocatchblockismatched,thefunctionterminatewillbeinvokedautomaticallyandtheprogramisaborted.1013.1ExceptionHandlingExample:#includeintDiv(intx,inty);voidmain()Onlythetypeof“y”ismatter!intDiv(intx,inty)tryif(y=0)cout5/2=Div(5,2)endl;cout8/0=Div(8,0)endl;cout7/1=Div(7,1)endl;throwy;returnx/y;Output:5/2=2exceptofdevidingzero.thatisok.catch(int)coutexceptofdevidingzero.n;coutthatisok.n;1113.2ExceptionMatchingYoucanlistalltypesofexceptionsinthedefinitionoffunction.Example:voidfun()throw(A,B,C,D);Youcanalsodefineafunctionthatcanthrowanytypeofexceptions.Example:voidfun()throw();1213.3ConstructorsandDestructorsinExceptionHandlingTheC+Spirit:“Trusttheprogrammer”.Anobjectcomesintobeingonlyafteritasbeenconstructed.1313.3ConstructorsandDestructorsinExceptionHandlingIfanexceptionismatchedbythecatchblock,somethingwillbedonetohandleit.A)Initializingthearguments.B)Alltheobjectsthathavebeenconstructedinthetryblockwillbedestructedautomatically.C)Thecodesegmentnexttothelastcatchblockwillbeinvoked.1413.3ConstructorsandDestructorsinExceptionHandlingExample:classXpublic:voidmain()Xx;ABCa;b;c;abc;1513.3ConstructorsandDestructorsinExceptionHandlingExample:#includevoidMyFunc(void);classExptpublic:Expt();Expt();constchar*ShowReason()constreturnExpt;16;13.3ConstructorsandDestructorsinExceptionHandlingclassDemopublic:Demo();Demo();Demo:Demo()coutConstructingDemo.endl;Demo:Demo()coutDestructingDemo.endl;1713.3ConstructorsandDestructorsinExceptionHandlingvoidMyFunc()DemoD;coutThrowExptinMyFunc()endl;throwExpt();intmain()Therewillbesomeerrors.coutInmainfunctionendl;trycoutIntryblock,MyFunc()isinvokedendl;MyFunc();1813.3ConstructorsandDestructorsinExceptionHandlingcatch(ExptE)coutIncatchblockendl;coutCatchingtheExpt;coutE.ShowReason()endl;catch(char*str)coutCatchingtheotherexception:strendl;coutBacktomainfunctionendl;return0;1913.3ConstructorsandDestructorsinExceptionHandlingOutput:InmainfunctionIntryblock,MyFunc()isinvokedConstructingDemo.ThrowExptinMyFunc()DestructingDemo.IncatchblockCatchingtheExptExptBacktomainfunction20Exercise#includeintmain()voidf1();tryf1();catch(double)cout“OK0!”endl;cout“end0”endl;lreturn0;21voidf1()voidf2();tryf2();catch(char)cout“OK1!”endl;cout“end1”endl;l22voidf2()voidf3();tryf3();catch(int)cout“OK2!”endl;cout“end2”endl;l23voidf3()doubled=0;trythrowd;catch(float)cout“OK3!”endl;cout“end3”endl;l24mainf1()f2()f3()throwdcatch(float)notmatchcatch(double)matchcatch(char)notmatchcatch(int)notmatchOutput:OK0!end0Whatwillhappeniffunctionf3sargumenttypeofcatchturnstobedouble?OK3!end3end2end1end025SummarizeExceptionHandlingExceptionMatchingConstructorsandDestructorsinExceptionHandling26
展开阅读全文