Catching an exception(cont u The type of exception objects will be compared at each catch sentence Once met, sentences in corresponding catch block will be executed a Program continues after all catch sentences are passed
Catching an exception (cont.) ◼ The type of exception objects will be compared at each catch sentence ◼ Once met, sentences in corresponding catch block will be executed ◼ Program continues after all catch sentences are passed
Catching an exception(cont In some cases, if we could not finish the complete handling of certain exceptions, we could re-throw them and let other catch -es? to handle it catch(iterator overflow& iof log message(iof. what happened) throw; //re-throw this exception, //it could appear only in catch sentence
Catching an exception (cont.) ◼ In some cases, if we could not finish the complete handling of certain exceptions, we could re-throw them and let other “catch-es” to handle it { //… catch (iterator_overflow& iof) { log_message(iof.what_happened()); throw; //re-throw this exception, //it could appear only in catch sentence } }
Catching an exception(cont J If We want to catch all types of exceptions, We could use catch-all sentence catch(.) log message"Exception of unknown type):
Catching an exception (cont.) ◼ If we want to catch all types of exceptions, we could use catch-all sentence { //… catch (…) { log_message(“Exception of unknown type”); //… } }
Trying for an exception u Catch sentences should be companion with try blocks a If there is something abnormal while the execution of the try block, it should be handled in catch sentence
Trying for an exception ◼ Catch sentences should be companion with try blocks ◼ If there is something abnormal while the execution of the try block, it should be handled in catch sentence
Trying for an exception(cont.) bool has elem(Fibonacci Iterator first, Fibonacci Iterator last, int elem) bool status=tr while(first !=last) if(first==elem) return status first++, i catch(iterator overflow& iof) //only catch exceptions of iterator overflow i log message(iof. what happened); j status= false return status
Trying for an exception (cont.) bool has_elem(Fibonacci_Iterator first, Fibonacci_Iterator last, int elem) { bool status = true; try { while (first != last) { if (*first == elem) return status; first++; } } catch (iterator_overflow& iof) //only catch exceptions of iterator_overflow { log_message(iof.what_happened()); } status = false; return status; }