slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Exception Handling PowerPoint Presentation
Download Presentation
Exception Handling

Loading in 2 Seconds...

play fullscreen
1 / 13

Exception Handling - PowerPoint PPT Presentation


  • 107 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Exception Handling' - fola


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
what is an exception
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
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 (...);

...}

slide4

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
General Format

Try {

c++ valid statements;

}

catch( )

{ error handling part;

}

catch(argument )

{ error handling part;

}

slide6

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

slide7

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

slide8

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;

example1
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
example2
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
slide11

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 };

slide12

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 }

slide13

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()