Chapter 16 Exception Handling 0 2018, SEU. All rights reserved. 1
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 1 Exception Handling Chapter 16
OBJECTIVES o What exceptions are and when to use them o To use Try, catch and throw to detect, handle and indicate exceptions, respectively. o To process uncaught and unexpected exceptions. o To declare new exception classes. o How stack unwinding enables exceptions not caught in one scope to be caught in another Scope o To handlle new failures o To use auto_ptr to prevent memory leaks To understand the standard exception hierarchy. 0 2018, SEU. All rights reserved. 2
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 2 OBJECTIVES What exceptions are and when to use them. To use Try, catch and throw to detect, handle and indicate exceptions, respectively. To process uncaught and unexpected exceptions. To declare new exception classes. How stack unwinding enables exceptions not caught in one scope to be caught in another scope. To handle new failures. To use auto_ptr to prevent memory leaks. To understand the standard exception hierarchy
Topics o 16.1 Introduction o 16.2 Exception-Handling overview 16.3 Example: Handling an Attempt to Divide by Zero o 16.4 When to Use Exception Handling o 16.5 Rethrowing an Exception 16.6 Exception Specifications o 16.7 Processing Unexpected Exceptions o16.8 Stack Unwinding 16.9 Constructors, Destructors and Exception Handling o 16.10 Exceptions and Inheritance o 16.11 Processing new Failures 0 2018, SEU. All rights reserved. 3
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 3 Topics 16.1 Introduction 16.2 Exception-Handling Overview 16.3 Example: Handling an Attempt to Divide by Zero 16.4 When to Use Exception Handling 16.5 Rethrowing an Exception 16.6 Exception Specifications 16.7 Processing Unexpected Exceptions 16.8 Stack Unwinding 16.9 Constructors, Destructors and Exception Handling 16.10 Exceptions and Inheritance 16.11 Processing new Failures
16.1 Introduction 大型程序往往会产生一些很难查找的甚至是无法 避免的运行时错误。 ●当发生运行时错误时,不能简单地结束程序运行, 而是需要指出错误,并由用户决定下一步工作。 面向对象的异常处理( exception handling)机 制是C++语言用以解决这个问题的有力工具。 0 2018, SEU. All rights reserved. 4
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 4 16.1 Introduction 大型程序往往会产生一些很难查找的甚至是无法 避免的运行时错误。 当发生运行时错误时,不能简单地结束程序运行, 而是需要指出错误,并由用户决定下一步工作。 面向对象的异常处理(exception handling)机 制是C++语言用以解决这个问题的有力工具
16.1 Introduction °异常( exception)是程序可能检测到的,运行时 不正常的情况,如存储空间耗尽、数组越界、被0 除等等。 °特点:可以预见可能发生在什么地方,但是无法 确知怎样发生和何时发生。 °在一个大型的程序(软件)中,程序各部分是由不同的 小组编写的,它们由公共接口连起来,错误可能就发生 在相互的配合上,也可能发生在事先根本想不到的个别 的条件组合上 0 2018, SEU. All rights reserved. 5
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 5 16.1 Introduction 异常(exception)是程序可能检测到的,运行时 不正常的情况,如存储空间耗尽、数组越界、被0 除等等。 特点:可以预见可能发生在什么地方,但是无法 确知怎样发生和何时发生。 在一个大型的程序(软件)中,程序各部分是由不同的 小组编写的,它们由公共接口连起来,错误可能就发生 在相互的配合上,也可能发生在事先根本想不到的个别 的条件组合上
Topics o 16.1 Introduction o 16.2 Exception-Handling Overview 16.3 Example: Handling an Attempt to Divide by Zero o 16.4 When to Use Exception Handling o 16.5 Rethrowing an Exception 16.6 Exception Specifications o 16.7 Processing Unexpected Exceptions o16.8 Stack Unwinding 16.9 Constructors, Destructors and Exception Handling o 16.10 Exceptions and Inheritance o 16.11 Processing new Failures 0 2018, SEU. All rights reserved. 6
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 6 Topics 16.1 Introduction 16.2 Exception-Handling Overview 16.3 Example: Handling an Attempt to Divide by Zero 16.4 When to Use Exception Handling 16.5 Rethrowing an Exception 16.6 Exception Specifications 16.7 Processing Unexpected Exceptions 16.8 Stack Unwinding 16.9 Constructors, Destructors and Exception Handling 16.10 Exceptions and Inheritance 16.11 Processing new Failures
16.2 Exception-Handling overview ∥直观的错误处理模式 Perform a task If the preceding task did not execute correctly Perform error processing else Perform next task If the preceding task did not execute correctly Perform error processing 将程序逻辑和错误处理逻辑混合,降低程序性能。 C++提供了异常处理模式!-将异常处理单独分离 0 2018, SEU. All rights reserved. 7
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 7 16.2 Exception-Handling Overview //直观的错误处理模式 Perform a task If the preceding task did not execute correctly Perform error processing else Perform next task If the preceding task did not execute correctly Perform error processing 将程序逻辑和错误处理逻辑混合,降低程序性能。 C++提供了异常处理模式!---将异常处理单独分离
16.2 Exception-Handling overview 解决思路:C++提供了一些内置的语言特性来抛出 ( throw)异常,用以通知“异常已经发生”,然 后由预先安排的程序段来捕获( catch)异常,并对 它进行处理。 ∥抛掷异常的程序段 捕获并处理异常的程序段 ry 复合语句} throw表达式; catch(异常类型声明1) {复合语句} 多cach(异常类型声明2) 个 {复合语句} 0 2018, SEU. All rights reserved. 8
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 8 16.2 Exception-Handling Overview //抛掷异常的程序段 ...... throw 表达式; ...... //捕获并处理异常的程序段 try {复合语句} catch(异常类型声明1) {复合语句} catch(异常类型声明2) {复合语句} … 解决思路:C++提供了一些内置的语言特性来抛出 (throw)异常,用以通知“异常已经发生” ,然 后由预先安排的程序段来捕获(catch)异常,并对 它进行处理。 多 个
16.2 Exception-Handling overview 流程解释 °将可能抛出异常的程序段嵌在try块之中,并通过 throw操 作创建一个异常对象并抛掷。 ●如果此时没有引起异常,程序从ry块后跟随的最后一个 catch子句后面的语句继续执行下去。 如果存在异常,顺序检查ty块后面的 catch子句,匹配的 catch子句将捕获并处理异常(或继续抛掷异常)。 ●如果匹配的处理器未找到,则默认调用 terminate函数,其 缺省功能是调用 abort终止程序。(具体在16.7节中阐述) 0 2018, SEU. All rights reserved. 9
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 9 16.2 Exception-Handling Overview --流程解释 将可能抛出异常的程序段嵌在try块之中,并通过throw操 作创建一个异常对象并抛掷。 如果此时没有引起异常,程序从try块后跟随的最后一个 catch子句后面的语句继续执行下去。 如果存在异常,顺序检查try块后面的catch子句,匹配的 catch子句将捕获并处理异常(或继续抛掷异常)。 如果匹配的处理器未找到,则默认调用terminate函数,其 缺省功能是调用abort终止程序。(具体在16.7节中阐述)
Topics o 16.1 Introduction o 16.2 Exception-Handling Overview 16.3 Example: Handling an Attempt to Divide by Zero o 16.4 When to Use Exception Handling o 16.5 Rethrowing an Exception 16.6 Exception Specifications o 16.7 Processing Unexpected Exceptions o16.8 Stack Unwinding 16.9 Constructors, Destructors and Exception Handling o 16.10 Exceptions and Inheritance o 16.11 Processing new Failures 0 2018, SEU. All rights reserved. 10
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 10 Topics 16.1 Introduction 16.2 Exception-Handling Overview 16.3 Example: Handling an Attempt to Divide by Zero 16.4 When to Use Exception Handling 16.5 Rethrowing an Exception 16.6 Exception Specifications 16.7 Processing Unexpected Exceptions 16.8 Stack Unwinding 16.9 Constructors, Destructors and Exception Handling 16.10 Exceptions and Inheritance 16.11 Processing new Failures