Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.

Similar presentations


Presentation on theme: "Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution."— Presentation transcript:

1 Exception Handling

2 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 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 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 5 Exception Handling 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 6 Exception Handling Exception handling code consists of the following segments To detect error To throw error To catch error To take appropriate action

7 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

8 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 { int a,b; …… if(a==0) { throw “Divide by Zero”; } cout<<“checking”; }

9 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 (int i) { // 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

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

11 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

12 12 Exception Handling Void main() { int a,b,c; cout >a>>b; try { if(b!=0) { c=a/b;cout<

13 13 Exception Handling Exception handling in function Void check(int x,int y) { if(y!=0) { int z=x/y;cout<

14 14 Exception Handling

15 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(int i) { 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 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 }

17 17 Exception Handling 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"; }


Download ppt "Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution."

Similar presentations


Ads by Google