Trying for an exception(cont.) inline int Fibonacci Iterator: operator O check integrity return Fibonacci s elems m idx I inline void Fibonacci Iterator: check integrity if(m idx >=1024) throw iterator overflow(m ild, 1024)
Trying for an exception (cont.) inline int Fibonacci_Iterator::operator*() { check_integrity(); return Fibonacci::s_elems[ m_iIdx ]; } inline void Fibonacci_Iterator::check_integrity() { if (m_iIdx >= 1024) throw iterator_overflow(m_iIdx, 1024); //… }
Trying for an exception(cont.) Suppose that the m dx exceeds the maximum limit 1024, What will happen? Exception handling facility will check whether the place where exceptions were thrown out is in a try block. If so, it tries to find a catch sentence that could handle this type of exceptions and do corresponding operations Programs continues after that
Trying for an exception (cont.) ◼ Suppose that the m_iIdx exceeds the maximum limit 1024, what will happen? ◼ Exception handling facility will check whether the place where exceptions were thrown out is in a try{} block. If so, it tries to find a catch sentence that could handle this type of exceptions and do corresponding operations. Programs continues after that
Trying for an exception(cont.) u But for our previous example, throw is not in a try block, exception handling facility will not handle this exception but terminate the execution of remaining codes, and try to find a corresponding catch in caller check _ integrity!()→ operator*()→ has elem a Programs will continue from status =false If an appropriate catch could not be found even in the end of the function calling chain -main, the terminated provided in the standard library will be executed, and the whole program terminated Rule in C++: every exception should be handled
Trying for an exception (cont.) ◼ But for our previous example, throw is not in a try{} block, exception handling facility will not handle this exception but terminate the execution of remaining codes, and try to find a corresponding catch in caller ◼ check_integrity() → operator*() → has_elem() ◼ Programs will continue from status = false; ◼ If an appropriate catch could not be found even in the end of the function calling chain – main(), the terminate() provided in the standard library will be executed, and the whole program terminated ◼ Rule in C++: every exception should be handled
Trying for an exception(cont.) u Why C++ not supports resuming from where exception Was thrown after the exception is handled? Exception handling facilty could not know how much clean-ups should be done to return to right state and continue the following lines If resuming is required, it is also required that programmer writing throw codes be familiar with catch codes which might be written by another programmer, and vice versa a That will bring complex depending relationship and maintenance problems, in a close coupling form If you really want a problem solving-resuming""mode, write a"check-restore' function before throwing an exception
Trying for an exception (cont.) ◼ Why C++ not supports resuming from where exception was thrown after the exception is handled? ◼ Exception handling facilty could not know how much clean-ups should be done to return to right state and continue the following lines ◼ If resuming is required, it is also required that programmer writing throw codes be familiar with catch codes which might be written by another programmer, and vice versa ◼ That will bring complex depending relationship and maintenance problems, in a close coupling form ◼ If you really want a problem “solving-resuming” mode, write a “check-restore” function before throwing an exception
Trying for an exception(cont.) u But that not means every function should handle every possible exception If certain sentence which not in a try block may lead to an exception, this exception surely would not be handled in this function Though function will be terminated, it will not always be hazard and cause problems(as the operator米( It depends on the programmer that which sentences should be in try block and which ones outside
Trying for an exception (cont.) ◼ But that not means every function should handle every possible exception ◼ If certain sentence which not in a try block may lead to an exception, this exception surely would not be handled in this function ◼ Though function will be terminated, it will not always be hazard and cause problems (as the operator*()) ◼ It depends on the programmer that which sentences should be in try block and which ones outside