当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

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

资源类别:文库,文档格式:PPT,文档页数:63,文件大小:840.5KB,团购合买
点击下载完整版文档(PPT)

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);

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共63页,可试读20页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有