• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Lecture16
 

Lecture16

on

  • 200 views

 

Statistics

Views

Total Views
200
Views on SlideShare
200
Embed Views
0

Actions

Likes
0
Downloads
3
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Lecture16 Lecture16 Presentation Transcript

    • Exception Handling Lesson # 16 1/15
    • 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
    • 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
    • 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
    • General FormatTry { c++ valid statements; }catch( ){ error handling part; }catch(argument ){ error handling part; } 5/15
    • # 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
    • 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
    • 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
    • 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 }
    • 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
    • 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
    • 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
    • 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