《The C++ Programming Language》课程教学资源(PPT课件讲稿)Lecture 07 Exception Handling

The c ++ Programming Language Lecture 7 EXception Handling
The C++ Programming Language Lecture 7: Exception Handling

Basic Concepts and Syntaxes
Basic Concepts and Syntaxes

Exception handing facility u As We are the designer and provider of our classes, we do know when they would be in invalid status or have problems, and could not be used any more a But we do not know the impact it would bring to user's program and its harm, only users know the degree a Our duty is to notify the user what happened when there was a problem by using the C++ exception handling facility, and let user decide how to response
Exception handling facility ◼ As we are the designer and provider of our classes, we do know when they would be in invalid status or have problems, and could not be used any more ◼ But we do not know the impact it would bring to user’s program and its harm, only users know the degree ◼ Our duty is to notify the user what happened when there was a problem by using the C++ exception handling facility, and let user decide how to response

Exception handling facility(cont.) Exception handling facility contains two phases Exception recognizing and throwing Exception handling EXception handling process Once an exception appears, the running program is suspended On the meanwhile, exception handling facility finds positions that have the ability to handle this type of exception After the exception is handled, the execution of program is resumed, and continues from the handling position
Exception handling facility (cont.) ◼ Exception handling facility contains two phases: ◼ Exception recognizing and throwing ◼ Exception handling ◼ Exception handling process ◼ Once an exception appears, the running program is suspended ◼ On the meanwhile, exception handling facility finds positions that have the ability to handle this type of exception ◼ After the exception is handled, the execution of program is resumed, and continues from the handling position

Throwing an exception Imaging our previous Fibonacci iterator example lass FIbonacci Iterator public CFibonacci Iterator(int idx): m idx(idx-1) bool operator=-(const CFibonacci Iterator&) const bool operator!= const CFibonacci Iterator&)const int operator Const FIbonacci Iterator& operator++O CFibonacci Iterator operator++(int private void check integrity int m iIdx
Throwing an exception ◼ Imaging our previous Fibonacci_Iterator example class CFibonacci_Iterator { public: CFibonacci_Iterator(int idx) : m_iIdx(idx - 1) {}; bool operator= =(const CFibonacci_Iterator&) const; bool operator!=(const CFibonacci_Iterator&) const; int operator*() const; CFibonacci_Iterator& operator++(); CFibonacci_Iterator operator++( int ); private: void check_integrity(); int m_iIdx; }

Throwing an exception(cont u midx may be set to certain value in constructor u Thus Iterator will have the potential danger that exceeds the allowed maximum size of static vector in Fibonacci u How to notify the user this exception? line void Fibonacci Iterator:check integrity if(m idx > Fibonacci m IMaxElems) throw iterator overflow(m idx, Fibonacci: m iMaxElems if(m idx >=FibonacciS elems size) Fibonacci gen elems(m idx 1)
Throwing an exception (cont.) ◼ m_iIdx may be set to certain value in constructor ◼ Thus Iterator will have the potential danger that exceeds the allowed maximum size of static vector in Fibonacci ◼ How to notify the user this exception? inline void Fibonacci_Iterator::check_integrity() { if (m_iIdx >= Fibonacci::m_iMaxElems) throw iterator_overflow(m_iIdx, Fibonacci::m_iMaxElems); if (m_iIdx >= Fibonacci::s_elems.size()) Fibonacci::gen_elems(m_iIdx + 1); }

Throwing an exception(cont u An exception is an object throw 42 throwpanic, no buffer! i In most cases, exceptions thrown belong to certain exception classes these classes may form a inheritance hierarchy a We define our iterator overflow exception class
Throwing an exception (cont.) ◼ An exception is an object throw 42; throw “panic: no buffer!”; ◼ In most cases, exceptions thrown belong to certain exception classes, these classes may form a inheritance hierarchy ◼ We define our iterator_overflow exception class

Throwing an exception(cont class iterator overflow publIc iterator overflow(int index, int max) m index(index), m max(max)i int index(f return m index; 1 int max(f return m max; j void what happened( ostream& os=cerr) os<<“ Internal error: current index”<< m index <"exceeds the maximum bound: '7<<m max private Int m index, m max
Throwing an exception (cont.) class iterator_overflow { public: iterator_overflow(int index, int max) : m_index(index), m_max(max) {}; int index() { return m_index; } int max() { return m_max; } void what_happened(ostream& os = cerr) { os << “Internal error: current index” << m_index << “exceeds the maximum bound: ” << m_max; } private: int m_index, m_max; }

Throwing an exception(cont Our previous throw example throws an anonymous object of iterator_overflow class and constructor is directly called Also we could clarify the exception object name inline void Fibonacci Iterator: check integrity if(m idx >=Fibonacci m iMaxElems) iterator overflowex(m idx, Fibonacci m iMaxElems) throw ex; i if(m idx > Fibonacci S elems size) Fibonacci gen elems(m idx+ 1)
Throwing an exception (cont.) ◼ Our previous throw example throws an anonymous object of iterator_overflow class, and constructor is directly called ◼ Also we could clarify the exception object name inline void Fibonacci_Iterator::check_integrity() { if (m_iIdx >= Fibonacci::m_iMaxElems) { iterator_overflow ex(m_iIdx, Fibonacci::m_iMaxElems); throw ex; } if (m_iIdx >= Fibonacci::s_elems.size()) Fibonacci::gen_elems(m_iIdx + 1); }

Catching an exception J Use single or multiple catch sentences to catch thrown exception objects extern void log message(const char*) catch(const char* str) extern string err message i log message(str) extern ostream log file status= false catch(iterator overflow& iof) bool funcAO i lof. what happened(log file) status=false; j bool status= true return statu catch(int errno) i log message(err message[errno].c strO) status=false; j a It could handle our previous 3 exception objects throw 42, throw“ panIc: no buffer!”; a throw iterator_overflow(m_iIdx, Fibonacci: m_iMaxElems)
Catching an exception ◼ Use single or multiple catch sentences to catch thrown exception objects extern void log_message(const char*); extern string err_message[]; extern ostream log_file; bool funcA() { bool status = true; //… catch (int errno) { log_message(err_message[errno].c_str()); status = false; } catch (const char* str) { log_message(str); status = false; } catch (iterator_overflow& iof) { iof.what_happened(log_file); status = false; } return status; } ◼ It could handle our previous 3 exception objects: ◼ throw 42; ◼ throw “panic: no buffer!”; ◼ throw iterator_overflow(m_iIdx, Fibonacci::m_iMaxElems);
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 香港城市大学:《计算机图形学》课程教学资源(PPT课件讲稿)图的算法 Graph Algorithms.ppt
- 白城师范学院:《数据库系统概论 An Introduction to Database System》课程教学资源(PPT课件讲稿)第五章 数据库完整性.pptx
- 《SQL Server 2000数据库教程》教学资源(PPT课件讲稿)第11章 数据库安全性管理.ppt
- PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING.ppt
- 南京大学:移动Agent系统支撑(PPT讲稿)Mobile Agent Communication——Software Agent.pptx
- 《计算机组成原理》课程教学资源(PPT课件讲稿)第三章 CPU子系统.ppt
- 长春工业大学:《Javascript 程序设计》课程教学资源(PPT课件讲稿)第8章 网页特效 JavaScript.ppt
- 江苏海洋大学(淮海工学院):《Java面向对象程序设计》课程教学资源(PPT课件讲稿)全国二级Java考试的重点难点.pptx
- 交互式数据语言(PPT讲稿)Basic IDL knowledge.ppt
- 电子科技大学:《计算机操作系统》课程教学资源(PPT课件讲稿)第二章 进程与调度(Processes and Scheduling).ppt
- 北京航空航天大学:《数据挖掘——概念和技术(Data Mining - Concepts and Techniques)》课程教学资源(PPT课件讲稿)Chapter 05 Mining Frequent Patterns, Association and Correlations.ppt
- 计算机算法(PPT讲稿)禁忌搜索算法 Tabu Search.ppt
- 2019年《计算机网络》考试大纲.doc
- 四川大学:《数据结构》课程教学资源(PPT课件讲稿)第五章 树和二叉树 Tree & Binary Tree.ppt
- 佛山科学技术学院:《网络技术基础》课程教学资源(专业技能考试大纲).doc
- 《计算机操作系统》课程教学资源(PPT课件讲稿)第二章 进程描述与控制 Process Concept & Process Control.ppt
- 香港城市大学:PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING.ppt
- 《计算机网络》课程教学资源(PPT课件讲稿)第8章 应用层.ppt
- 并行处理(PPT讲稿)Parallel Processing - Hypercubes and Their Algorithms.ppt
- 《计算机网络》课程电子教案(PPT课件讲稿)第2章 数据通信的基础知识.ppt
- 《C语言程序设计》课程教学资源(PPT课件讲稿)第9章 用户自己建立数据类型.pptx
- 《计算机网络教程》课程PPT教学课件(第三版)第3章 网络体系结构与网络协议.ppt
- 西安交通大学:《物联网技术导论》课程教学资源(PPT课件)第一章 物联网技术概论(主讲:桂小林).ppt
- 电子科技大学:《计算机操作系统》课程教学资源(PPT课件讲稿)第二章 进程与调度 Processes and Scheduling.ppt
- 《Web网站设计与开发》课程教学资源(PPT课件讲稿)第10章 Java Web实用开发技术.ppt
- 可信计算 Trusted Computing(PPT讲稿)TSS - TCG Software Stack.ppt
- 西安电子科技大学:《现代密码学》课程教学资源(PPT课件讲稿)第一章 绪论(主讲:董庆宽).pptx
- 《VB程序设计》课程教学资源(PPT课件讲稿)第二章 VB语言基础.ppt
- 《计算机网络》课程教学大纲 Computer Networks.pdf
- 《Photoshop教程》教学资源(PPT课件)第6章 Photoshop的绘图工具.ppt
- 《高级语言程序设计》课程教学资源(试卷习题)试题二(无答案).doc
- 机械工业出版社:国家“十一五”规划教材《数据库原理与应用教程》教学资源(PPT课件,第3版)第4章 数据操作.ppt
- 厦门大学:Web技术(PPT课件讲稿)网站快速开发 & Web前端技术.ppt
- 《面向对象技术》课程教学大纲 Technology of Object-Oriented Programming.doc
- 《算法分析与设计》课程教学资源(PPT课件讲稿)第六章 基本检索与周游方法(一般方法).ppt
- 南京大学:《面向对象技术 OOT》课程教学资源(PPT课件讲稿)设计模式 Design Patterns(1).ppt
- 上海交通大学:IT项目管理(PPT讲稿)讲座5 目标、范围管理与需求工程.ppt
- 《面向对象建模技术》课程教学资源(PPT课件讲稿)第11章 UML与RUP.ppt
- 中国科学技术大学:《网络信息安全 NETWORK SECURITY》课程教学资源(PPT课件讲稿)第九章 网络攻击.ppt
- 西安电子科技大学:《Mobile Programming》课程PPT教学课件(Android Programming)Lecture 04 Activity, Intent and UI.pptx