《软件开发》课程PPT教学课件:Chapter 16 异常处理 Exception Handling

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
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 西安电子科技大学:《计算机网络 Computer Networks》课程教学资源(PPT课件讲稿)基于CORBA的分布式平台(CORBA编程-Hello World例程).ppt
- 电子工业出版社:《计算机网络》课程教学资源(第五版,PPT课件讲稿)第七章 网络安全.ppt
- 北京大学:浅谈计算机研究的层次与境界(李振华).pptx
- 南京大学:《计算机图形学》课程教学资源(PPT课件讲稿)计算机图形学引言(主讲:路通).ppt
- 国家十一五规划教材:《电子商务案例分析》课程教学资源(PPT课件)第11章 网络社区模式案例分析.ppt
- 西安电子科技大学:《操作系统 Operating Systems》课程教学资源(PPT课件讲稿)Chapter 08 多处理器系统 Multiple Processor Systems.ppt
- 计算机问题求解(PPT讲稿)图论中的其它专题.pptx
- SIGCOMM 2002:New Directions in Traffic Measurement and Accounting.ppt
- 厦门大学计算机科学系:《大数据技术原理与应用》课程教学资源(PPT课件)第十章 数据可视化.ppt
- 成都信息工程大学(成都信息工程学院):分层分流培养个性发展的计算机卓越工程师——专业课分层教学探索与实践.ppt
- 沈阳理工大学:《Java程序设计基础》课程教学资源(PPT课件讲稿)第1章 创建Java开发环境.ppt
- 北京师范大学网络教育:《计算机应用基础》课程教学资源(PPT讲稿)第8章 计算机安全、第9章 多媒体技术.pptx
- 西安电子科技大学:《8086CPU 指令系统》课程教学资源(PPT课件讲稿,共五部分,王晓甜).pptx
- 北京大学:《搜索引擎 Search Engines》课程教学资源(PPT讲稿)Evaluating Search Engines(Search Engines Information Retrieval in Practice).ppt
- 香港浸会大学:《Data Communications and Networking》课程教学资源(PPT讲稿)Chapter 4 Transmission Media.ppt
- 《EDA技术》实用教程(PPT讲稿)第5章 QuartusII 应用向导.ppt
- 《数字图像处理 Digital Image Processing》课程教学资源(PPT课件讲稿)第2章 图像分析.ppt
- 东南大学计算机学院:《操作系统概念 OPERATING SYSTEM CONCEPTS》课程教学资源(PPT课件)Operating-System Structures.ppt
- 《计算机组装与维修》课程教学资源(PPT课件讲稿)第二章 计算机系统维护维修工具使用.ppt
- 《数据结构》课程教学资源:课程PPT教学课件:绪论(数据结构讨论的范畴、基本概念、算法和算法的量度).ppt
- 《Adobe Photoshop CS》软件教程(PPT讲稿)第13章 使用路径.ppt
- Virtual Topologies - Faculty of Science, HKBU.ppt
- 《Java语言程序设计》课程教学资源(PPT课件讲稿)第三章 面向对象特征.ppt
- 中国科学技术大学:《算法基础》课程教学资源(PPT课件讲稿)第七讲 顺序统计学(主讲人:吕敏).pptx
- 清华大学出版社:《C语言程序设计》课程教学资源(PPT课件讲稿)第7章 用户自定义函数.ppt
- 清华大学:Mandarin Pronunciation Variation Modeling.ppt
- 西安电子科技大学:《MATLAB程序设计语言》课程教学资源(PPT讲稿)Chapter1 Matlab系统概述.ppt
- 中国科学技术大学:《网络算法学》课程教学资源(PPT课件)第六章 传输控制.ppt
- 香港浸会大学:《Data Communications and Networking》课程教学资源(PPT讲稿)Socket Programming Part II:Design of Server Software.ppt
- 上海交通大学:《软件开发》课程教学资源(PPT课件)第一讲 概述.ppt
- 《计算机网络原理》课程教学资源(PPT课件讲稿)第二章 网络实现模型.ppt
- 香港理工大学:INSTRUCTION SETS 指令.pptx
- 计算机问题求解(PPT讲稿)B树.pptx
- 北京大学远程教育:《计算机应用基础》课程PPT教学课件(专科)串讲(综合复习).pptx
- 《Microsoft Access 2003》教程PPT:第9章 报表设计.ppt
- 《编译原理和技术》课程PPT教学课件:第十三章 函数式语言的编译.ppt
- 四川大学:Object-Oriented Design and Programming(Java,PPT课件).ppt
- 安徽理工大学:《汇编语言》课程教学资源(PPT课件讲稿)第五章 循环与分支程序设计.ppt
- 《C程序设计》课程PPT教学课件(电子教案)第六章 函数.ppt
- 基于语义关联和信息增益的TFIDF改进算法研究.ppt