Your SlideShare is downloading. ×
0
Lecture16
Lecture16
Lecture16
Lecture16
Lecture16
Lecture16
Lecture16
Lecture16
Lecture16
Lecture16
Lecture16
Lecture16
Lecture16
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Lecture16

115

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
115
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  1. Exception Handling Lesson # 16 1/15
  2. 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. 2/15
  3. Example Index out of rangeDivision by zero int array [10];float x, y; for (int i=0; i<=10; i++)…. array [i] = something;y = 0.0;float result = x/y No Space class Student {...} int main () {Student *aStudent; aStudent = new Student (...); ...} 3/15
  4. To enable the program to take care (handle)of suchexceptional situations, 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 throw3. 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 4/15
  5. General FormatTry { c++ valid statements; }catch( ){ error handling part; }catch(argument ){ error handling part; } 5/15
  6. # include <iostream.h> We write the code int main() in a try block{ int value1, value2, result; try { cin >> value1; cin >> value2; If there is an exception, we throw it to a handler if (value2 == 0) { throw ; } If there is no exception, we resume the execution result = value1/value2; cout <<"result is :"<< result; }// end of try catch ( ) { cout << " just cannot divide by zero"; }// end of catch 6/15}// end of main
  7. 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 parametersin the throw, i.e., OVERLOADING 7/15
  8. int main(){ int value1, value2, result; catch ( ) {cout << " just cannot divide by zero"; try }// end of catch {cin >> value1;cin >> value2; catch (int v ) if (value1 < 0) {cout << v << "is less than zero, {throw (value1); can’t you see?"; } }// end of catch if (value2 == 0) {throw ; … } return 0; result = value1/value2; }// end of main cout <<"result is :"<< result; }// end of try 8/15
  9. ExampleInt main ( ) will this CATCH work ?{ try{ Int main ( ) cout<<“inside try”; { try{ throw 100; cout<<“inside try”; cout<<“will this execute”; throw 100; } cout<<“will this execute”; }catch(int I) {cout <<“the caught an exception of value”<<I; } catch(double I) {} cout <<“the caught an exception of value”<<I; } 9/15 }
  10. ExampleVoid 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;} } 10/15
  11. EXAMPLE#include <iostream.h>void MyFunc( void );CT 1 class CTestCT 2 {CT 3 public:CT 4 CTest(){};CT 5 ~CTest(){};CT 6 const char * ShowReason( ) constCT 7 { return "Exception in CTest class."; }CT8 }; 11/15
  12. CD 1 class CDtorDemoCD 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 } 12/15
  13. int main(){1 cout << "In main." << endl;2 try3 { cout << "In try block, calling MyFunc()."<<endl;4 MyFunc();5 } // end try6 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() 13/15

×