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