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