Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exception Handling How to handle the runtime errors.

Similar presentations


Presentation on theme: "Exception Handling How to handle the runtime errors."— Presentation transcript:

1 Exception Handling How to handle the runtime errors

2 1. What is Exception? Exception means Errors 2. When does the exception occur ? At 2 Places I) at compile time II) at runtime Corrected by the programmer The compiler will not be able to find them or correct them. Syntax Errors LogicalErrors&FaultyInput

3 This makes programs more robust This makes programs more robust 1. Your Program does not terminate when a wrong input is given 2. Your program itself will identify what type of error has occurred and it will inform the user to correct himself What is the use of exception Handling? What kinds of exceptions does C++ can handle? C++ has exception handling features to handle all kinds of errors Some Situations where an exception may occur - Next Slide

4 Some of the Situations when an exception may occur class Student {...} int main () {Student *aStudent; aStudent = new Student (...);... } Division by zero float x, y; …. y = 0.0; float result = x/y Index out of range int array [10]; for (int i=0; i<=10; i++) array [i] = something; Cannot divide by zero Array index are from 0 to 9. There is no such element with index 10 It might be the case where there is no enough space in memory to dynamically allocate memory spaces to aStudent.

5 Functions provided by C++ for exception handling 1. We write all the statements which we feel may generate an error during runtime into the try block 2.A try block can be followed by one or more Catch Blocks There are three functions in C++ to handle exceptions, they are 1.try 2. Catch 3.throw What is a try block ?

6 main ( ) { int a; cin >> a; cout << a; } When to use try block ? If the Input is : 10 Output is : 10 No Problem If the Input is : A Sample Run 1 Sample Run 2 Program Stops What should I do? main ( ) {int a; try { cin >> a; cout << a; }} Use Try and Catch Blocks in the Program

7 Catch is used to catch an exception which has occurred within the statements written within the try block What is a Catch Block ? What to write within the catch block? The Code to be executed when an error occurs How many catch blocks to write ? One catch block for one type of exception

8 How to Use a catch block with a try block ? main ( ) {int a; try { cin >> a; cout << a; } catch( ) { cout<<“Enter Numeric Data”; }} Will catch be executed when a Numeric Data is entered? For Every Catch Block there should be parameter

9 How to use many catch blocks with one try block ? Try { // statements that may generate exceptions } Catch (type 1 argument) { // error handling code; } Catch (type 2 argument) { // error handling code; } Catch (type 3 argument) { // error handling code; } Which catch block will be instigated? (invoked) The Execution of the catch block depends on the TYPE of the exception generated

10 What is throw ? It is a built in function to raise or generating an exception by the programmer himself. Syntax: throw (argument); optional throw; What will happen ? So an exception is generated and it will be caught by the catch block.

11 # include # include int main() int main() {int value1, value2, result; {int value1, value2, result; }// end of main try { cin >> value1; cin >> value2; }// end of try catch ( ) { cout << " just cannot divide by zero"; { cout << " just cannot divide by zero"; }// end of catch }// end of catch if (value2 == 0) { throw ; } result = value1/value2; cout <<"result is :"<< result; If there is an exception, we throw it to a handler We write the code in a try block If there is no exception, we resume the execution

12 try {cin >> value1;cin >> value2; }// end of try if (value2 == 0) {throw ; {throw ; } result = value1/value2; cout <<"result is :"<< result; if (value1 < 0) {throw (value1); {throw (value1); } catch ( ) {cout << " just cannot divide {cout << " just cannot divide by zero"; by zero"; }// end of catch }// end of catch catch (int v ) {cout << v << "is less than zero, {cout << v << "is less than zero, can’t you see?"; can’t you see?"; }// end of catch }// end of catch int main() { int value1, value2, result; … return 0; }// end of main

13 What is the Output ? void main( ) { try { cout << “inside try block”; throw 50; cout << “end”; } catch (int c) { cout << “catch type 1”; } catch (float c) { cout << ‘catch type 2”; } } statement Will this statement be executed?

14 - What is the output ? # include void Xhandler( int test) { try { if (test ==0) throw 0; if (test ==1) throw ‘a’; if (test ==0) throw 123.33;} catch ( int i) { cout << “first type”;} catch ( char x) { cout << “second type”;} catch ( double y) { cout << “third type”;} } Int main( ) { cout << “start”; Xhandler(0); Xhandler(1); Xhandler(2); cout << “end; }

15 Example - What is the output ? # include void Xhandler( int test) { try { if (test ==0) throw 0; if (test ==1) throw ‘a’; if (test ==0) throw 123.33;} catch (...) { cout << “Caught an exception”; } Int main( ) { cout << “start”; Xhandler(0); Xhandler(1); Xhandler(2); cout << “end; } Catch all Exceptions

16 Restricting Exceptions Can restrict the type of the exceptions a function can throw # include # include void Xhandler( int test)throw(int,char,double) try { try { if (test ==0) throw 0; if (test ==1) throw ‘a’; if (test ==0) throw 123.33;} catch ( int i) { cout << “first type”;} catch ( char x) { cout << “second type”;} catch ( double y) { cout << “third type”; } } Int main( ) { cout << “start”; Xhandler(0);Xhandler(1);Xhandler(2); cout << “end;} throw (typelist)

17 Throwing an exception from within a catch block is called rethrowing. Int main( ) { try { xhandler( ); } catch(const char *) { cout << “caught char * inside main”; } Void xhandler ( ) { try { throw “hello”;} catch (const char *) { cout << “insidexhandler”; throw; } What is Re throwing ?

18 #include class Test { public: Test(){}; ~Test(){}; void ShowReason( ) { cout << " in Test class."; } }; int main( ) { try { throw TEST( ); // statements } catch (TEST E) {cout << “in main”; E.showreason( ); }} How to throw class as exceptions?

19 Computer Programming II 19 #include #include class DivideByZeroException { public: DivideByZeroException() : message( "attempted to divide by zero" ) { } DivideByZeroException() : message( "attempted to divide by zero" ) { } const char *what() const { return message; } const char *what() const { return message; }private: const char *message; const char *message;}; double quotient( int numerator, int denominator ) { if ( denominator == 0 ) if ( denominator == 0 ) throw DivideByZeroException(); throw DivideByZeroException(); return static_cast ( numerator ) / denominator; return static_cast ( numerator ) / denominator;} Example 2

20 Computer Programming II 20 int main() { int number1, number2; double result; cout << "Enter two integers (end-of-file to end): "; while ( cin >> number1 >> number2 ) { try { result = quotient( number1, number2 ); cout << "The quotient is: " << result << endl; } catch ( DivideByZeroException ex ) { // exception handler cout << "Exception occurred: " << ex.what() << '\n'; } cout << "\nEnter two integers (end-of-file to end): "; } cout << endl; return 0; // terminate normally }


Download ppt "Exception Handling How to handle the runtime errors."

Similar presentations


Ads by Google