




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、C+ Programming CHAPTER 13 EXCEPTION HANDLING 113.1 Exception Handling13.2 Exception Matching 13.3 Constructors and Destructors in Exception Handling213.1 Exception HandlingAn exception is a run-time error caused by some abnormal condition.In C+, a function f can recognize conditions that identify ex
2、ceptions and then signal that an exception has occurred. This signaling process is called throwing an exception.313.1 Exception HandlingOnce thrown, an exception can be caught or handled by a function that invokes f by using a catch block. A catch block is an exception handler that occurs after a tr
3、y block, which is used to indicate interest in exceptions. 413.1 Exception Handling f() h()g()error spreadingfunction invokedcatching and handling errorcause and throw an errorWhy h( ) doest rectify the error?int *p=new int1000513.1 Exception HandlingThe perfect objective was to map exceptions to so
4、me other form of error propagation should a designer choose to do so.Not that it was always best to do so, but that it could be done.613.1 Exception Handlingthrowing exception .throw expression;.catching and handling exceptiontry expressioncatch(exception type) expressioncatch(exception type) expres
5、sion .713.1 Exception HandlingThe keyword throw creates an object that isnt there under normal program execution. Then the object is, in effect, “returned” from the function, even though that object type isnt normally what the function is designed to return.Each catch clause (exception handler) is l
6、ike a little function that takes a single argument of one particular type.813.1 Exception HandlingRemarks:A) Exception is made and thrown by throw block. B) The code segment that could cause an exception will be in the try block. C) The catch block will not invoked if there is not any exception in t
7、ry block, and the code next to the last catch block will be invoked.913.1 Exception HandlingD) If an exception is thrown, type-matched catch block will catch and handle it. E) If no catch block is matched, the function terminate will be invoked automatically and the program is aborted.1013.1 Excepti
8、on HandlingExample:#includeint Div(int x,int y);void main() try cout5/2=Div(5,2)endl;cout8/0=Div(8,0)endl;cout7/1=Div(7,1)endl; catch(int) coutexcept of deviding zero.n; coutthat is ok.n;int Div(int x,int y) if(y=0) throw y; return x/y;Output:5/2=2except of deviding zero.that is ok.Only the type of
9、“y” is matter!1113.2 Exception MatchingYou can list all types of exceptions in the definition of function.Example:void fun( ) throw(A,B,C,D);You can also define a function that can throw any type of exceptions.Example:void fun( ) throw( );1213.3 Constructors and Destructors in Exception HandlingThe
10、C+ Spirit:“ Trust the programmer”.An object comes into being only after it as been constructed.1313.3 Constructors and Destructors in Exception HandlingIf an exception is matched by the catch block, some thing will be done to handle it.A) Initializing the arguments. B) All the objects that have been
11、 constructed in the try block will be destructed automatically. C) The code segment next to the last catch block will be invoked.1413.3 Constructors and Destructors in Exception HandlingExample:class X public:Aa;Bb;Cc;void main( )X x;abc1513.3 Constructors and Destructors in Exception HandlingExampl
12、e:#include void MyFunc( void );class Expt public: Expt( ); Expt( ); const char *ShowReason( ) const return Expt; ;1613.3 Constructors and Destructors in Exception Handlingclass Demo public:Demo( );Demo( );Demo:Demo( ) coutConstructing Demo.endl;Demo:Demo( ) coutDestructing Demo.endl;1713.3 Construct
13、ors and Destructors in Exception Handlingvoid MyFunc( ) Demo D; coutThrow Expt in MyFunc() endl; throw Expt();int main( ) coutIn main function endl; try coutIn try block, MyFunc( ) is invoked endl; MyFunc( ); There will be some errors.1813.3 Constructors and Destructors in Exception Handling catch(
14、Expt E ) coutIn catch block endl; coutCatching the Expt ; coutE.ShowReason()endl; catch( char *str ) coutCatching the other exception:strendl; coutBack to main function endl; return 0;1913.3 Constructors and Destructors in Exception HandlingOutput:In main functionIn try block, MyFunc() is invokedCon
15、structing Demo.Throw Expt in MyFunc()Destructing Demo.In catch blockCatching the Expt ExptBack to main function20Exercise#include int main()void f1();tryf1();catch(double)cout“OK0!”endl;cout“end0”endl;lreturn 0;21void f1()void f2();tryf2();catch(char)cout“OK1!”endl;cout“end1”endl;l22void f2()void f3();tryf3();catch(int)cout“OK2!”endl;cout“end2”endl;l23void f3()double d=0;trythrow d;catch(float)cout“OK3!”endl;cout“end3”endl;l24mainf1()f2()f3()throw dcatch(char)not matchcatch(int)not matchca
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 促銷(xiāo)活動(dòng)促銷(xiāo)活動(dòng)效果對(duì)消費(fèi)者購(gòu)買(mǎi)決策的影響分析考核試卷
- 風(fēng)險(xiǎn)評(píng)估框架構(gòu)建考核試卷
- 部編人教版一年級(jí)語(yǔ)文上冊(cè)全冊(cè)各單元知識(shí)點(diǎn)單元復(fù)習(xí)卡
- 部分醫(yī)療服務(wù)項(xiàng)目?jī)r(jià)格調(diào)整表
- 部編版中考道德與法治一輪復(fù)習(xí)|七年級(jí)下冊(cè)第四單元 走進(jìn)法治天地 復(fù)習(xí)學(xué)案+試卷
- 2025年中國(guó)L型單主梁吊鉤門(mén)式起重機(jī)數(shù)據(jù)監(jiān)測(cè)報(bào)告
- 2025年中國(guó)EVT扭力尺數(shù)據(jù)監(jiān)測(cè)報(bào)告
- 2025年中國(guó)BOPP雙向拉伸印刷膜數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 2025至2030年中國(guó)GPS衛(wèi)星導(dǎo)航定位電子板市場(chǎng)分析及競(jìng)爭(zhēng)策略研究報(bào)告
- 2025至2030年中國(guó)隱蔽式鉸鏈?zhǔn)袌?chǎng)分析及競(jìng)爭(zhēng)策略研究報(bào)告
- 2025年 中國(guó)南水北調(diào)集團(tuán)新能源投資公司第一批中層及考試筆試試卷附答案
- 期末試卷(五)(含答案含聽(tīng)力原文無(wú)聽(tīng)力音頻)-2024-2025學(xué)年人教PEP版英語(yǔ)(新教材)三年級(jí)下冊(cè)
- 湖南2024生地會(huì)考試卷及答案
- 廣東省深圳市2024年中考英語(yǔ)真題(含答案)
- 敘事護(hù)理學(xué)智慧樹(shù)知到答案2024年中國(guó)人民解放軍海軍軍醫(yī)大學(xué)
- 六年級(jí)主題班隊(duì)會(huì)記錄表(6個(gè)表)
- 方案軌道交通pis+cctv系統(tǒng)解決方案淺析
- 證明(員工喪事請(qǐng)假村委會(huì)出具證明)
- 重客渠道基本法
- 水果切自動(dòng)售貨機(jī)項(xiàng)目籌劃案(現(xiàn)實(shí)案例內(nèi)含運(yùn)營(yíng)表格)
- 《高階譜分析》ppt課件
評(píng)論
0/150
提交評(píng)論