软件设计(II)教学课件:Chapter13 Exception Handling.pptVIP

  • 1
  • 0
  • 约7.33千字
  • 约 28页
  • 2021-11-30 发布于安徽
  • 举报

软件设计(II)教学课件:Chapter13 Exception Handling.ppt

Chapter 13 Exception Handling (异常处理) §13.1 Introduction §13.2 Exception Handling Mechanism §13.3 Throwing Mechanisms §13.4 Catching Mechanisms §13.5 Rethrowing an Exception §13.6 Specifying Exception * §13.1 Introduction A program to read in two integers and displays their quotient * Quotient Run int main(){ cout Enter two integers: ; int num1, num2; cin num1 num2; cout num1/num2 is (num1 / num2) endl; return 0; } If you enter “0” for num2, then? A runtime error! An exception! What’s Exception? Peculiar problems other than logic or syntax errors Run time anomalies or unusual conditions Usually cause the program terminate abnormally Including: division by zero, access to an array outside of its bounds, or running out of memory or disk space, etc. * Naive Exception Handling Example of Integer Division Adding an if statement to test the second number * if (num2 != 0){ cout num1 / num2 is (num1 / num2) endl; }else cout Divisor cannot be zeroendl; QuotientWithIf Run §13.2 Exception Handling Mechanism Special Mechanism to detect, report and act against exceptions. Four tasks Find the problem(Hit the exception) Inform that an error has occurred (Throw the exception) Receive the error information (Catch the exception) Take corrective actions (Handle the exception) * Try-throw-catch The try block: To preface a block of statements which may generate exceptions. The throw statement: To report the exception by throwing a object. The catch block: To catche the exception thrown and take actions to handle it. * try block Detects and throws an exception catch block Catches and handles the exception Exception object C++ Exception Handling Using try-throw-catch * int main(){ cout Enter two integers: ; int num1, num2; cin num1 num2; try{ if (num2 == 0) throw num1; cout num1 / num2 is (num1 / num2) endl; }catch (int e){ cout Exception: an integer

文档评论(0)

1亿VIP精品文档

相关文档