收藏 分销(赏)

C--程序设计教学课件:CHAPTER-13-EXCEPTION-HANDLING.pptx

上传人:二*** 文档编号:5455732 上传时间:2024-11-05 格式:PPTX 页数:26 大小:540.36KB 下载积分:5 金币
下载 相关 举报
C--程序设计教学课件:CHAPTER-13-EXCEPTION-HANDLING.pptx_第1页
第1页 / 共26页
本文档共26页,全文阅读请下载到手机保存,查看更方便
资源描述
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
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服