130 likes | 258 Views
Exception Handling. Lesson # 16. What is an Exception ?. 1 .When a program is executed, unexpected situation may occur. Such a situation is called an exception . EG : a) indexing outside the limits in an array, b) giving faulty input data
E N D
Exception Handling Lesson # 16
What is an Exception ? 1 .When a program is executed, unexpected situation may occur. Such a situation is called an exception. EG : a) indexing outside the limits in an array, b) giving faulty input data c) failure of new to obtain a requested amount of memory. 2. An exception is not necessarily the result of a logic error in the program. It also can arise from faulty input data.
Example Index out of range int array [10]; for (int i=0; i<=10; i++) array [i] = something; Division by zero float x, y; …. y = 0.0; float result = x/y No Space class Student {...} int main () {Student *aStudent; aStudent = new Student (...); ...}
To enable the program to take care (handle)of such exceptionalsituations, C++ provides the following features: 1.Try Block - The code which might generate a runtime error is written within the try block. 2.Throw The programmer can generate an exception using throw 3.Catch Block Catches the error which may be generated from the code within the try block. A try block should be followed by one or more catch blocks. General Format - Next Slide
General Format Try { c++ valid statements; } catch( ) { error handling part; } catch(argument ) { error handling part; }
We write the code in a try block If there is an exception, we throw it to a handler If there is no exception, we resume the execution # include <iostream.h> int main() { int value1, value2, result; }// end of main try { cin >> value1; cin >> value2; }// end of try if (value2 == 0) { throw ; } result = value1/value2; cout <<"result is :"<< result; catch ( ) { cout << " just cannot divide by zero"; }// end of catch
Some times, we might have many different exceptions 1. We should write as many catch blocks. 2. This means also that we should have as many throw statements. 3. BUT(usually), only one try. But, which catch block will be instigated? (invoked) The conflict will be eliminated depending on the parameters in the throw, i.e., OVERLOADING
int main() { int value1, value2, result; catch ( ) {cout << " just cannot divide by zero"; }// end of catch try {cin >> value1;cin >> value2; }// end of try catch (int v ) {cout << v << "is less than zero, can’t you see?"; }// end of catch if (value1 < 0) {throw (value1); } if (value2 == 0) {throw ; } … return 0; }// end of main result = value1/value2; cout <<"result is :"<< result;
Int main ( ) { try{ cout<<“inside try”; throw 100; cout<<“will this execute”; } catch(int I) { cout <<“the caught an exception of value”<<I; } } will this CATCH work ? Int main ( ) { try{ cout<<“inside try”; throw 100; cout<<“will this execute”; } catch(double I) { cout <<“the caught an exception of value”<<I; } } Example
Void xtest(int test) { cout <<“inside Xtest”<<test; if (test) throw test; } int main( ) { try { cout<<“inside try”; xtest(0); xtest(1); xtest(2); } catch (int I) {cout<<“inside catch”<<I;} } Example
EXAMPLE #include <iostream.h> void MyFunc( void ); CT 1 class CTest CT 2 { CT 3 public: CT 4 CTest(){}; CT 5 ~CTest(){}; CT 6 const char * ShowReason( ) const CT 7 { return "Exception in CTest class."; } CT8 };
CD 1 class CDtorDemo CD 2 { public: CD 3 CDtorDemo(); CD 4 ~CDtorDemo(); CD 5 }; CD 6 CDtorDemo::CDtorDemo() CD 7 {cout << "Constructing CDtorDemo." << endl;} CD 8 CDtorDemo::~CDtorDemo() CD 9 {cout << "Destructing CDtorDemo." << endl;} My 1 void MyFunc() My 2 { CDtorDemo D; My 3 cout<< "In MyFunc(). Throwing CTest exception." << endl; My 4 throw CTest(); My 5 }
int main() { 1 cout << "In main." << endl; 2 try 3 { cout << "In try block, calling MyFunc()."<<endl; 4 MyFunc(); 5 } // end try 6 catch( CTest E ) 7 { cout << "In catch handler." << endl; 8 cout << "Caught CTest exception type: "; 9 cout << E.ShowReason() << endl; 10 } //end catch( CTest E ) 11 catch( char *str ) 12 {cout << "Caught some other exception: " << str << endl; } 13 cout << "Back in main. Execution resumes here." << endl; 14 return 0; 15 }// end main()