Exception Handling
2 Two types of bugs (errors) Logical error Syntactic error Logical error occur Due to poor understanding of the problem and solution procedure Syntactic error occur Due to poor understanding of language, can solve by compiler
3 Exception Handling Exceptions are runtime errors Such as division by zero, array out of bounds, no disk space, trying to read/write data from improper address (or an unopened file)… When a run time error encountered the program terminates C++ provides built in features to detect & handle exceptions
4 Exception Handling Exceptions are 2 types Synchronous exceptions Asynchronous exceptions Synchronous out of range index, overflow, divide by zero.. Asynchronous beyond the control of the program such as keyboard problem…. Exception handling mechanism mainly for Synchronous errors
5 Exception Handling mechanism Exception handling mechanism detect and report an runtime error, so appropriate action can be taken Exception handling mechanism steps 1)Find an error (hit exception) 2)Inform that an error occurred (throw exception) 3)Receive the error information (catch exception) 4)Take corrective action(handle exception)
6 Exception Handling mechanism Exception handling code consists of the following segments To detect error To throw error To catch error To take appropriate action
7 Exception Handling mechanism Exception handling mechanism done with three keywords try throw catch
8 Exception Handling try block A block of statements surrounded with braces, which may generate error If suspect a statement or a set of statements may cause any runtime error, group them in a block and name it as try block try { statements; }
9 Throwing Exceptions When an error is detected in the try block it is thrown using a throw statement in the try block try { int a,b; …… if(a==0) { throw “Divide by Zero”; } cout<<“checking”; }
10 Catching Exceptions: Defined by the keyword catch Catches the exception thrown by the throw statement in the try block The catch block that catches an exception must immediately follow the try block Any thrown exception must be caught by a catch statement that has thrown the exception try { int a,b; …… if(a==0) { throw “Divide by Zero”; } cout<<“checking”; } catch (type arguments) { statments }
11 Exception Handling main() { cout << "Start\n"; try { // start a try block cout << "Inside try block\n"; throw (100); // throw an error cout << "This will not execute"; } catch (int i) { // catch an error cout << "Caught an exception -- value is: "; cout << i << "\n"; } cout << "End“; } This program displays the following output: Inside try block Caught an exception -- value is: 100 End
12 Exception Handling When try block throws an exception the program control leaves the try block and enters the catch statements After exception handling the program returns to the point after the try-catch block, not after the throw statement. Exceptions are objects used to transmit information about a problem If the type object throw matches the argument type in the catch, then catch block is executed for handling the error If they do not matches program terminates
13 Exception Handling void main() { int a,b,c; cout >a>>b; try { if(b!=0) { c=a/b;cout<<c; } else { throw(b); //throw int object } catch(int x) // catches the exception { cout<<“exception caught “ <<x; } cout<<“END”; }
14 Exception handling in function void check(int x,int y) { if(y!=0) { int z=x/y;cout<<z; } else { throw(y); } void main() { try { check(3,0); } catch(int z) { cout<<“Caught the exception”; } cout<<“END” getch(); }
15 Multiple catch Statements We can have more than one catch associated with a try. However, each catch must catch a different type of exception. For example, this program catches both integers and strings. try { if(test) throw test; else throw "Value is zero"; } catch(int i) { cout << "Caught Exception #: " << i << '\n'; } Catch(char *str) { cout << "Caught a string: "; cout << str << '\n'; }
16 Catching All Exceptions In some situation we want an exception handler to catch all exceptions Using one catch block catch(...) { // process all exceptions }
17 Catching All Exceptions try { if(test==0) throw test; // throw int if(test==1) throw 'a'; // throw char if(test==2) throw ; // throw double } catch(...) { // catch all exceptions cout << "Caught One!\n"; }
18 Rethrowing an Exception Used when an exception handler cannot process an exception Rethrow exception with the statement: t hrow Without arguments Current exception thrown to the next enclosing try/catch sequence and is caught by a catch statement listed after that enclosing try block
19 Rethrowing an Exception void show(int a,int b) { cout<<“inside function”; try { if(b==0) { throw(b); } else { cout<<a/b;} } catch(int x) { cout<<“ caught an exception”; throw; } cout<<“end of function” } void main() { try { show(10,0); } catch(int x) { cout<<“caught exception inside main”; } output inside function caught an exception caught exception inside main
20 Rethrowing an Exception When an exception is rethrown, it will not be caught by the same catch or any other catch in that group It will be caught by an outer catch statements
21 Restricting Exception We can restrict the type of exception that a function can throw To accomplish this restriction add a throw clause to the function definition Syntax data type function(parameters) throw(data type list) { function body } Here only those data types that contain in the throw will be throw by the function Attempt to call a type that not in the throw, will terminate the program
22 Restricting Exception Void function(int x) throw(int, char) { if(x==1){ throw(1);} if(x==2){throw(3.5);} if(x==3){throw(‘a’);} } Void main() { try { function(1); function(2); function(3); } catch(int) { cout<<“caught an int”; } catch(char) { cout<<“caught a char”; } catch(float) { cout<<“caught a float”; } When the function called function(2) The program will terminate Because the function restricted only to caught int and char
23 Standard Library Exception Hierarchy Base-class exception – Contains virtual function what for storing error messages Exception classes derived from exception – bad_alloc – thrown by new – bad_cast – thrown by dynamic_cast – bad_typeid – thrown by typeid – bad_exception – thrown by unexpected Instead of terminating the program or calling the function specified by set_unexpected Used only if bad_exception is in the function’s throw list
24 Standard Library Exception Hierarchy
25 bad_exception Derived from the exception class This is a special type of exception specifically designed to be listed in the dynamic-exception of a function (i.e., in its throw specifier). Throws an exception not listed in it and unexpected rethrows it, a bad_exception is automatically thrown. void myfunction () throw (int, exception::bad_exception) { throw 'x'; // throws char (not in exception-specification) }
26 bad_exception void myfunction () throw (int, exception::bad_exception) { throw 'x'; // throws char (not in exception-specification) } Void main() { try { myfunction(1); } catch(exception::bad_exception) { cout<<“caughr bad exception”; } catch(char) { cout<<“caught a char”; }
27 Exception Handling