软件设计课件:Lecture 13 CPP Exceptions.pptxVIP

  • 1
  • 0
  • 约5.3千字
  • 约 52页
  • 2021-11-30 发布于安徽
  • 举报
Lecture 13. C++ Exceptions;Outline;Function calling Activation records;Evaluate x, in the callers context Store the caller’s current address in the callee’s AR Push the AR onto the call stack and switch execution to f() f() executes with its AR at the top of the call stack When f() is finished, it exits by popping its AR off the stack and returns to the caller using the previously stored return address;Function calling Activation records;The problem of unexpected issues;The problem of unexpected issues;Outline;What are Exceptions and Exception handling? Exceptions indicate problems that occur during a program’s execution, and the problems are special that they are unexpected and occur infrequently. Exception handling means resolve exceptions and make the programs robust and fault-tolerant ( allow a program to continue executing or notify the client code of the problem).;When shall we use Exception handling? After distinguishing exceptions from errors. Exception is a special kind of error which the code knows can foresee its happening but CANNOT handle and so far is supposed to be handled by some other code else. For example, when client code want to create a file. Exceptions can be happened in the callee when: 1. the disk is full 2. do not have permission to create a file. ;常见的异常 系统资源不足 1)内存不足:不可以动态申请内存空间 2)磁盘不足:不能创建新的文件 用户操作导致运算关系不正确 如分母为0、数学运算溢出、数组越界、参数类型不能转换等。 异常的特点 偶然性和可预见性——异常的存在和出现可以预见,但不会总是发生。 不可处理性——当前代码不知道如何处理,需要客户代码(调用者)来处理。 ; throwing an exception: Some library software or your code signals(发出信号) that something unusual has happened. handling the exception: At some other place in your program you place the code that deals with the exceptional case. ;异常处理方法 在calling stack中某个caller里面处理 不做处理(Exception最终将被main抛出给操作系统,导致程序终止执行) 处理方式 输出/记录错误信息,然后终止程序的运行 输出/记录错误信息,修复程序状态,然后继续运行程序;异常处理的思想;try, catch and throw;try, catch and throw;Outline;栈展开 抛出异常的时候,将暂停当??函数的执行,开始查找匹配的catch子句。首先检查throw本身是否在try 块内部,如果是,检查与该catch相关的catch子句,看是否其中之一与抛出对象相匹配。如果找到匹配的 c

文档评论(0)

1亿VIP精品文档

相关文档