Exception Handling BY Ms. P.Jayalakshmi, Ap/Cse Ms. J.Gowthamy, Ap/Cse
Exception Handling BY Ms. P.Jayalakshmi, Ap/Cse Ms. J.Gowthamy, Ap/Cse
BY
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.,
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
#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
catch(type2 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.
#include<iostream> using
namespace std; void test(int x)
{
try
{
if(x==1)
throw x; // throws an integer else
if( x==0)
{
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(…)
{
// Statements for processing all the exceptions.
}
void test(int x)
{
try
{
if(x==1)
cout<<”Caught an exception”<<endl;
int main()
{
cout<<”x==1”<<endl;
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:
#include<iostream>
{
try
if(y==0)
{
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