210 likes | 390 Views
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.
E N D
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
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
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
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
Exception Handling Exception handling code consists of the following segments To detect error To throw error To catch error To take appropriate action • popo
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
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
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
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
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
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
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
Exception Handling • popo
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.
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
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