




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 微課在高中物理教學中的應用
- 中藥炮制模擬考試題(附參考答案)
- 市場調查預測測試題(含參考答案)
- 2025年西藏日喀則區南木林高級中學高三第二次聯考英語試卷含答案
- 江西省上饒市2024-2025學年高一下學期4月期中地理試題(原卷版+解析版)
- 液化石油氣企業成本控制與預算管理考核試卷
- 棉花加工機械的制造信息化平臺建設考核試卷
- 玻璃制造中的光電器件應用考核試卷
- 皮具修理的工藝保護與可持續發展考核試卷
- 船舶拆除相關法律考核試卷
- 有機肥料腐熟度識別技術規范 -DB37-T 4110-2020 山東
- 2025年光大環保(中國)有限公司招聘筆試參考題庫含答案解析
- 《S市某大學宿舍樓工程招標控制價文件編制》13000字(論文)
- 啤酒廠精釀生產線安全操作規程
- 南京鐵道職業技術學院招聘筆試真題2023
- 外研版(2025新版)七年級下冊英語Unit 1 學情調研測試卷(含答案)
- T-GXAS 530-2023 桑螟防治技術規程
- 樁基礎工程施工進度計劃及工期保證措施
- 《智能建造技術與裝備》 課件 第一章 緒論
- 崗位職責會議組織模板
- 《邊防檢查法律法規》課件
評論
0/150
提交評論