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