13 exception handling

407 views

Published on

exception handling

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
407
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
25
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

13 exception handling

  1. 1. 09/04/131 VIT - SCSE • Logic errors • Syntactic errors • Mechanisms for handling run time errors • 2 types of exceptions • Synchronous exception • e.x: errors such as “out-of-range index” and “over-flow” • Asynchronous exception • e.x: such as a keyboard interrupts Exception Handling
  2. 2. 09/04/132 VIT - SCSE The proposed method is only for handling synchronous exceptions 1.find the problem (hit the exception) 2.inform that an error has occurred (throw the exception) 3.receive the error information (catch the exception) 4.take corrective actions (handle the exception)
  3. 3. 09/04/133 VIT - SCSE try block Detects and throw an exception catch block Catches and handles the exception
  4. 4. 09/04/134 VIT - SCSE …… …… try { …….. throw exception; …….. …….. } catch(type arg) { ……. ……. } ……. …….
  5. 5. 09/04/135 VIT - SCSE void main() { int i=0; cin>>i; try { if(i=0) throw i; } catch(int i) { cout<<”Exception caught: ; } cout<<”End”; }
  6. 6. 09/04/136 VIT - SCSE Throwing mechanism throw(exception); throw exception; throw;
  7. 7. 09/04/137 VIT - SCSE try { //try block } catch(type1 arg) { //catch block1 } catch(type2 arg) { //catch block2 } ……. ……. catch(typeN arg) { //catch blockN } Multiple catch statement
  8. 8. 09/04/138 VIT - SCSE void test(int x) { try { if(x= =1) throw x; else if(x= =0) throw ‘x’; else if(x= =-1 throw 1.0; cout<<”End of try block”; } catch(char c) { cout<<”Catch a character”; } catch(int m) { cout<<”catch an integer”; } catch(double d) { cout<<”catch a double”; } cout<<”End of try – catch system”; }
  9. 9. 09/04/139 VIT - SCSE void main() { cout<<”Testing multiple catches”; cout<<”x= =1 n”; test(1); cout<<”x= =0 n”; test(0); cout<<”x= =-1 n”; test(-1); cout<<”x= =2 n”; test(2); } Catch all exceptions catch(….) { //statements }
  10. 10. 09/04/1310 VIT - SCSE void test(int x) { try { if(x= =0) throw x; if(x= =-1) throw ‘x’; if(x= =1) throw 1.0; } catch(…) { cout<<”caught an exception”; } } void main() { cout<<”testing generic catch”; test(-1); test(0); test(1); } Handling Uncaught Exceptions terminate() set_terminate() unexpected() set_unexpected()
  11. 11. 09/04/1311 VIT - SCSE terminate() •the function terminate() is invoked when an exception is raised and the handler is not found. •The default action for terminate is to invoke abort(). •Such a default action causes immediate termination of the program execution.
  12. 12. 09/04/1312 VIT - SCSE class excep1 { }; class excep2 { }; void main() { try { cout<<”Throwing uncaught exception”<<endl; throw excep2(); } catch(excep1) //true if throw excep1 is executed in try scope { //action for exception cout<<”Exception1”; } //excep2 is not caught hence, program aborts here without proceeding further cout<<”I am not displayed”; }
  13. 13. 09/04/1313 VIT - SCSE Output: Throwing uncaught exception Program aborted
  14. 14. 09/04/1314 VIT - SCSE set_terminate user can define their own exception handler or user defined exception set_terminate is defined in except.h
  15. 15. 09/04/1315 VIT - SCSE The program to handle uncaught exceptions with the user specified terminate function //all exceptions are not caught, executes MyTerminate() #include<process.h> #include<except.h> class excep1 { }; class excep2 { }; void MyTerminate() { cout<<”My Terminate is invoked”; exit(1); }
  16. 16. 09/04/1316 VIT - SCSE void main() { set_terminate(MyTerminate); //set to our own terminate function try { cout<<”Throwing uncaught exception”<<endl; throw excep2(); } catch(excep1) //true if throw excep1 is executed in try scope { //action for exception cout<<”Caught exception1”; } //program abort() here, MyTransaction will be called cout<<”I am not displayed”; }
  17. 17. 09/04/1317 VIT - SCSE Output: Throwing uncaught exception My Terminate is invoked
  18. 18. 09/04/1318 VIT - SCSE unexpected() The unexpected () function is called when a function throws an exception not listed in its exception specification. The program calls unexpected() which calls any user-defined function registered by set_unexpected(). If no function is registered with set_unexpected, the unexpected() function then invokes the terminate() function. The prototype of the unexpected() call is void unexpected(); the function unexpected() returns nothing (void). But it can throw an exception through the execution of a function registered by the set_unexpected function.
  19. 19. 09/04/1319 VIT - SCSE //unexpected exception #include<except.h> class zero { }; void dis(int num) throw() { if(num>0) cout<<"+ve number"; else if(num<0) cout<<"-ve number"; else throw zero(); //unspecified exception } void main() { int num; cout<<"Enter any number"; cin>>num; try { dis(num); } catch(...) { cout<<"catch all exceptions"; } cout<<endl<<"end of main"; getch(); }
  20. 20. 09/04/1320 VIT - SCSE Output 1: Enter any number 10 +ve number end of main Output 2: Enter any number -5 -ve number end of main Output 3: Enter any number 0 Program aborted
  21. 21. 09/04/1321 VIT - SCSE set_unexcepted() #include<process.h> //has prototype for exit() #include<except.h> class zero { }; void dis(int num) throw() { if(num>0) cout<<"+ve number"; else if(num<0) cout<<"-ve number"; else throw zero(); //unspecified exception } //this is automatically called whenever an unexpected exception occurs void MyUnexpected() { cout<<”My unexpected handler is invoked”; exit(34); } void main() { int num; cout<<"Enter any number"; cin>>num; set_unexpected(MyUnexpected); //user defined handler
  22. 22. 09/04/1322 VIT - SCSE try { dis(num); } catch(...) { cout<<"catch all exceptions"; } cout<<endl<<"end of main"; getch(); } Output 1: Enter any number 10 +ve number end of main Output 2: Enter any number -5 -ve number end of main Output 1: Enter any number 0 My unexpected handler is invoked

×