Exception Handling in C++
Exception Handling in C++
Exception is a runtime error. Exception handling is a mechanism that allows you to take
appropriate action to avoid runtime errors.
1. Try : The try block contain statements which may generate exceptions.
2. Throw : When an exception occur in try block, it is thrown to the catch block using
throw keyword.
3. Catch :The catch block defines the action to be taken, when an exception occur.
#include<iostream.h>
#include<conio.h>
void main()
{
int n1,n2,result;
cout<<"\nEnter 1st number : ";
cin>>n1;
cout<<"\nEnter 2nd number : ";
cin>>n2;
try
{
if(n2==0)
throw n2; //Statement 1
else
{
result = n1 / n2;
cout<<"\nThe result is : "<<result;
}
}
catch(int x)
{
cout<<"\nCan't divide by : "<<x;
}
}
Output :
Enter 1st number : 45
Enter 2nd number : 0
Can't divide by : 0
Data-type in Catch block specifies the type of exception that catch block will handle, Catch
block will receive value, send by throw keyword in try block.
A single try statement can have multiple catch statements. Execution of particular catch
block depends on the type of exception thrown by the throw keyword. If throw keyword send
exception of integer type, catch block with integer parameter will get execute.
#include<iostream.h>
#include<conio.h>
void main()
{
int a=2;
try
{
if(a==1)
throw a; //throwing integer exception
else if(a==2)
throw 'A'; //throwing character exception
else if(a==3)
throw 4.5; //throwing float exception
}
catch(int a)
{
cout<<"\nInteger exception caught.";
}
catch(char ch)
{
cout<<"\nCharacter exception caught.";
}
catch(double d)
{
cout<<"\nDouble exception caught.";
}
cout<<"\nEnd of program.";
}
Output :
Character exception caught.
End of program.
The above example will caught only three types of exceptions that are integer, character and
double. If an exception occurs of long type, no catch block will get execute and abnormal
program termination will occur. To avoid this, we can use the catch statement with
three dots as parameter (...) so that it can handle all types of exceptions.
#include<iostream.h>
#include<conio.h>
void main()
{
int a=1;
try
{
if(a==1)
throw a; //throwing integer exception
else if(a==2)
throw 'A'; //throwing character exception
else if(a==3)
throw 4.5; //throwing float exception
}
catch(...)
{
cout<<"\nException occur.";
}
cout<<"\nEnd of program.";
}
Output :
Exception occur.
End of program.
Rethrowing Exceptions
Rethrowing exception is possible, where we have an inner and outer try-catch statements
(Nested try-catch). An exception to be thrown from inner catch block to outer catch block is
called rethrowing exception.
#include<iostream.h>
#include<conio.h>
void main()
{
int a=1;
try
{
try
{
throw a;
}
catch(int x)
{
cout<<"\nException in inner try-catch block.";
throw x;
}
}
catch(int n)
{
cout<<"\nException in outer try-catch block.";
}
cout<<"\nEnd of program.";
Output :
Exception in inner try-catch block.
Exception in outer try-catch block.
End of program.
We can restrict the type of exception to be thrown, from a function to its calling statement, by
adding throw keyword to a function definition.
Example of restricting exceptions
#include<iostream.h>
#include<conio.h>
void Demo() throw(int)
{
int a=2;
if(a==1)
throw a; //throwing integer exception
else if(a==2)
throw 'A'; //throwing character exception
else if(a==3)
throw 4.5; //throwing float exception
}
void main()
{
try
{
Demo();
}
catch(int n)
{
cout<<"\nException caught.";
}
cout<<"\nEnd of program.";
}
The above program will terminate, because we have restricted the Demo() function to throw
only integer exceptions and Demo() is throwing character type exception.