- 1
- 0
- 约7.33千字
- 约 28页
- 2021-11-30 发布于安徽
- 举报
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
您可能关注的文档
最近下载
- 企业研发活动统计107-1、107-2报表版整理版(2021年-2022年).pdf VIP
- XX中学校2026年春季学期安全工作计划与应急演练安排表.docx VIP
- 园林植物配置与应用PPT课件.pptx VIP
- DB33/1006-2017 浙江省住宅设计标准.docx VIP
- HGT 4684-2014 液氯泄漏的处理处置方法.docx VIP
- IMT_2030(6G)推进组-通信行业:6G可信内生安全架构研究报告.docx VIP
- 亨格瑞成本与管理会计(第16版) 课件 Chapter 1 The Manager and Management Accounting .pptx
- 人教版高中化学选择性必修第2册 第三章 晶体结构与性质 第三节 金属晶体与离子晶体(一).ppt VIP
- 2025年全媒体运营师全媒体5G传播应用与传统媒体传输方式对比专题试卷及解析.pdf VIP
- IMT_2030(6G)推进组-通信行业:6G通信感知一体化评估方法研究报告.docx VIP
原创力文档

文档评论(0)