軟件設(shè)計(jì)(II)教學(xué)課件:Chapter13 Exception Handling_第1頁
軟件設(shè)計(jì)(II)教學(xué)課件:Chapter13 Exception Handling_第2頁
軟件設(shè)計(jì)(II)教學(xué)課件:Chapter13 Exception Handling_第3頁
軟件設(shè)計(jì)(II)教學(xué)課件:Chapter13 Exception Handling_第4頁
軟件設(shè)計(jì)(II)教學(xué)課件:Chapter13 Exception Handling_第5頁
已閱讀5頁,還剩23頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、Chapter 13 Exception Handling (異常處理)13.1 Introduction13.2 Exception Handling Mechanism13.3 Throwing Mechanisms13.4 Catching Mechanisms13.5 Rethrowing an Exception 13.6 Specifying Exception113.1 IntroductionA program to read in two integers and displays their quotient2QuotientRunint main() cout num1

2、num2; cout num1/num2 is (num1 / num2) endl; return 0;If you enter “0” for num2, then?A runtime error!An exception!Whats Exception?Peculiar problems other than logic or syntax errorsRun time anomalies or unusual conditionsUsually cause the program terminate abnormallyIncluding:ldivision by zero,lacce

3、ss to an array outside of its bounds, or lrunning out of memory or disk space,letc.3Naive Exception HandlingExample of Integer DivisionlAdding an if statement to test the second number4 if (num2 != 0) cout num1 / num2 is (num1 / num2) endl; else cout Divisor cannot be zeroendl;QuotientWithIfRun13.2

4、Exception Handling MechanismSpecial Mechanism to detect, report and act against exceptions.Four taskslFind the problem(Hit the exception)lInform that an error has occurred (Throw the exception)lReceive the error information (Catch the exception)lTake corrective actions (Handle the exception)5Try-thr

5、ow-catchThe try block:lTo preface a block of statements which may generate exceptions.The throw statement:lTo report the exception by throwing a object.The catch block:lTo catche the exception thrown and take actions to handle it.6 try blockDetects and throwsan exception catch blockCatches and handl

6、esthe exceptionException objectC+ Exception HandlingUsing try-throw-catch7int main() cout num1 num2; try if (num2 = 0) throw num1; cout num1 / num2 is (num1 / num2) endl; catch (int e) cout Exception: an integer e cannot be divided by zero endl; cout Execution continues . side = side; double getSide

7、() return side; private: double side;Notes for Multiple CatchesIf a catch block catches exception objects of a base class, it can catch all the exception objects of the derived classes of that base class.So, a catch block for a base class type should appear after a catch block for a derived class ty

8、pe. try . catch (logic_error &ex) . catch (TriangleException &ex) . (a) Wrong order try . catch (TriangleException &ex) . catch (logic_error &ex) . (b) Correct order 1914.5 Rethrowing an ExceptionAn exception can be rethrownlBy a handlerlFor two reasonsThe current handler cannot proc

9、ess the exception or The current handler wants to let its caller be notified of the exception. The syntax:20try statements; catch (TheException &ex) perform operations before exits; throw;Program 13.5.cpp.htmlException Propagation int main() . try . invoke function1; statement1; catch (Exception

10、1 &ex1) Process ex1; statement2; function1 . try . invoke function2; statement3; catch (Exception2 &ex2) Process ex2; statement4; function2 . try . invoke function3; statement5; catch (Exception3 &ex3) Process ex3; statement6; Exception thrown in function3 Call Stack main main function1

11、main function1 main function1 function2 function2 function3 Invocation Exception 21Exception PropagationAn exception that cant be handled by any of a functions catch blocks is thrown to the callerThe exception is propagated along the reverse order of the function calling2214.6 Specifying ExceptionWh

12、at try-catch block should be provided in a function?Exceptions thrown by lThe function itselflThe functions calledWhat exceptions may be thrown by a function?23Exception SpecificationAlso known as throw listllists exceptions that a function can throw24returnType functionName(parameterList) throw (ex

13、ceptionList);Program 13.6.cpp.htmlTwo Special ListsNo throw listlMay throw any exception!Empty throw listlCant throw any exception!25returnType functionName(parameterList) throw ();returnType functionName(parameterList);Undeclared ExceptionThrowing an exception not declared in the throw list? The fu

14、nction unexpected will be invoked! lA standard C+ functionlNormally terminates the program26Visual studio doesnt satisfy the throw list specification well.Catch All Types of ExceptionsThe exception type may not be able to anticipateTo force a catch block to catch all exceptions thrown:catch() /Statements for processin

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論