1 / 17

Exception Handling

Exception Handling. popo. Exception Handling. 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. popo.

finian
Download Presentation

Exception Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exception Handling • popo

  2. Exception Handling 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 • popo

  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 • popo

  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 • popo

  5. Exception Handling Exception handling mechanism detect and report an runtime error, so appropriate action can be taken Exception handling mechanismsteps • Find an error (hit exception) • Inform that an error occurred (throw exception) • Receive the error information (catch exception) • Take corrective action(handle exception) • popo

  6. Exception Handling Exception handling code consists of the following segments To detect error To throw error To catch error To take appropriate action • popo

  7. Exception Handling Exception handling mechanism done with three keywords try throw catch try block A block of statements surrounded with braces, which may generate error If you suspect a statement or a set of statements may cause any runtime error, group them in a block and name it as try • popo

  8. Exception Handling try { statements; } throw When an error is detected in the try block it is thrown using a throw statement in the try block try { inta,b; …… if(a==0) { throw “Divide by Zero”; } cout<<“checking”; } • popo

  9. 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 (inti) { // catch an error • cout << "Caught an exception -- value is: "; • cout << i << "\n"; • } • cout << "End“; • This program displays the following output: • Start • Inside try block • Caught an exception -- value is: 100 • End • popo

  10. Exception Handling catch 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 { inta,b; …… if(a==0) { throw “Divide by Zero”; } cout<<“checking”; } catch (type arguments) { statments } • popo

  11. Exception Handling When try block throws an exception the program control leaves the try block and enteres the catch statements 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 • popo

  12. Exception Handling Void main() { inta,b,c; cout<<“enter 2 nos “;cin>>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”; } • popo

  13. Exception Handling Exception handling in function Void check(intx,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(); } • popo

  14. Exception Handling • popo

  15. Exception Handling • Using Multiple catch Statements • As stated, you can have more than one catch associated with a try. • In fact, it is common to do so. • 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(inti) • { cout << "Caught Exception #: " << i << '\n'; • } • catch(const char *str) • { cout<< "Caught a string: "; cout << str << '\n'; • } • Each catch statement responds only to its own type.

  16. Exception Handling • Catching All Exceptions • In some circumstances you will want an exception handler to catch all exceptions • This is easy to accomplish. • Simply use this form of catch. • catch(...) • { // process all exceptions • } • popo

  17. Exception Handling • try • { if(test==0) throw test; // throw int if(test==1) throw 'a'; // throw char if(test==2) throw 123.23; // throw double • } • catch(...) { // catch all exceptions • cout << "Caught One!\n"; • } • popo

More Related