0% found this document useful (0 votes)
66 views9 pages

Exception Handling BY Ms. P.Jayalakshmi, Ap/Cse Ms. J.Gowthamy, Ap/Cse

The document discusses exception handling in C++. It defines exceptions as runtime anomalies or unusual conditions encountered during program execution. There are two types of exceptions: asynchronous caused by events beyond program control, and synchronous caused by abnormal program conditions. Exception handling involves finding the problem, informing of the error, receiving error information, and taking corrective action. It uses try, throw, and catch blocks - try contains code that may cause exceptions, throw passes exceptions to catch, and catch handles exceptions. Multiple catch blocks can handle different exception types. Exceptions can also be rethrown without processing in the catch block.

Uploaded by

JAYALAKSHMI P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
66 views9 pages

Exception Handling BY Ms. P.Jayalakshmi, Ap/Cse Ms. J.Gowthamy, Ap/Cse

The document discusses exception handling in C++. It defines exceptions as runtime anomalies or unusual conditions encountered during program execution. There are two types of exceptions: asynchronous caused by events beyond program control, and synchronous caused by abnormal program conditions. Exception handling involves finding the problem, informing of the error, receiving error information, and taking corrective action. It uses try, throw, and catch blocks - try contains code that may cause exceptions, throw passes exceptions to catch, and catch handles exceptions. Multiple catch blocks can handle different exception types. Exceptions can also be rethrown without processing in the catch block.

Uploaded by

JAYALAKSHMI P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 9

EXCEPTION HANDLING

BY

Ms. P.JAYALAKSHMI, AP/CSE


Ms. J.GOWTHAMY, AP/CSE
EXCEPTION HANDLING

Exceptions are run-time anomalies or unusual conditions that are encountered while executing a program.
 For example, the run-time anomalies include:
 Division by zero.
 Access to an array outside its bounds
 Running out of memory and disk space.
 Exceptions are of two kinds:
1. Asynchronous exceptions

 These types of exceptions are caused by events beyond the control of the
program. For example: keyboard interrupts, hardware failure etc.,
2. Synchronous exceptions

 These types of exceptions are caused due to the abnormal conditions occurring
in a program. For example: out-of-range index, division by zero etc.,

Basics of Exception Handling:

The exception handling mechanism performs the following tasks:

1. Find the problem( Hit the exception)


2. Inform that an error has occurred ( Throw the exception)
3. Receive the error information (Catch the exception)
4. Take corrective actions (Handle the exception)

EXCEPTION HANDLING MECHANISM:


 The C++ Exception Handling mechanism operates on three keywords.
1. Try
2. Throw
3. Catch
2
try block

Detects and Throws


an exception
Exception
object

catch block
Catches and handles the exception

Try:
 The try block contains a block of statements which may generate exceptions.
Throw:
 When an exception is detected by the try block, it is thrown to the catch block using the throw
statement using the one of the following syntax:
throw exception;
throw; // used for rethrowing an exception.
Catch:
 The catch block contains the code for handling the exceptions. The catch block has the following
syntax:
catch (datatype arg)
{
//Statements for managing exceptions
}
 The datatype of argument is mandatory and argument name is optional.

 When the try block throws an exception, the program control leaves the try block and enters the
catch block.
The general form of these blocks is stated below:
try
{
…..
throw exception;
…..
}
catch( type arg)
{
......
}
3

EXAMPLE 1: THROW OUTSIDE THE TRY BLOCK

#include<iostream>
using namespace std;
void divide(int x, int y, int z)
{
if ((x-y)!=0)
{
int res=z/(x-y);
cout<<”Result =”<<res<<endl;
}
else
{
throw (x-y); // throwing integer
}
}
int main()
{
try
{
divide(10,20,30); //Invokes divide() function
divide(10,10,20); //Invokes divide() function
}
catch( int i) OUTPUT:
{
Result= -3
cout<<”Caught an exception”<<endl;
Caught an exception
}
return 0;
}
MUTLIPLE CATCH STATEMENTS:

A program segment may raise more than one exception during its execution. In such cases, multiple catch
statements should be used as shown below.
4

Syntax for multiple catch statements: try

catch (type1 arg)

catch(type2 arg)

……

catch( typeN arg)

{
}

 When an exception is thrown, the appropriate exception handlers (catch blocks) are searched for an
appropriate match.
 The first handler (catch block) that yields the exact match will be executed.
 When no match is found the program is terminated.

EXAMPLE FOR MULTIPLE CATCH STATEMENTS:

#include<iostream> using
namespace std; void test(int x)
{
try
{
if(x==1)
throw x; // throws an integer else
if( x==0)

throw ‘x’; // throws a character


5

else if(x== -1)


throw 1.0; // throws a float value
}
catch( char c) // Catch block1
{
cout<<”Caught a character”<<endl;
}
catch( int m) // Catch block2
{
cout<<”Caught an integer”<<endl;
}
catch (float f) // Catch block3

{
cout<<” Caught a float value”<<endl;
}
}
int main()
{
cout<<”x==1”<<endl;
test(1); // Invokes test() function
cout<<”x==0”<<endl;;
test(0); // Invokes test() function
cout<<”x==-1”<<endl;
test(-1); // Invokes test() function
return 0;
}

OUTPUT:
x==1
Caught an integer
x==0
Caught a character
x==-1
Caught a float value
6

Catch all exceptions:


The catch block with the following syntax catches all the exceptions instead of a certain type alone.

catch(…)
{
// Statements for processing all the exceptions.
}

EXAMPLE: CATCH ALL EXCEPTIONS


#include<iostream>

using namespace std;

void test(int x)
{
try

{
if(x==1)

throw x; // throwing integer

else if( x==0)

throw ‘x’; // throwing character

else if(x== -1)

throw 1.0; // throwing float


}
catch( …) // Catch block to catch all exceptions

cout<<”Caught an exception”<<endl;

int main()

{
cout<<”x==1”<<endl;

test(1); // Invokes test() function


cout<<”x==0”<<endl;

test(0); // Invokes test() function


cout<<”x==-1”<<endl;

test(-1); // Invokes test() function

return 0;

OUTPUT:
x==1
Caught an exception x==0
Caught an exception x== -1
Caught an exception

RETHROWING AN EXCEPTION

The catch block may decide to rethrow the exception caught without processing it. In such cases the throw
statement takes the following form:

Syntax for rethrow:

throw; //Throw without arguments

EXAMPLE: RETHROWING AN EXCEPTION

#include<iostream>

using namespace std;

void divide(int x, int y)

{
try

if(y==0)

throw y; // Throwing integer


else
cout<<”Result=”<<x/y<<endl;
}
catch(int)
{
cout<<”Caught integer inside divide()”<<endl;
throw; // Rethrowing exception
}
}
int main()

{
try

{
divide(10,5); // Invokes divide() function
divide(20,0); // Invokes divide() function
}
catch(int)
{
cout<<”Caught integer inside main()”<<endl;
}
return 0;
}

OUTPUT:
Result=2
Caught integer inside main

You might also like