website logo

Exception Handling in C++

What is Exception : Exception are runtime unexpected , unusual condition or error that occurred in a piece of code while program execution. that alter the normal program execution flow. some unusual condition like division by zero, array index outside of its bound or program executing out of memory disk space problem etc are condition of exception generation.
exception can occurs through hardware failures, bugs in program, resource exhaustion etc. exceptions are mainly other then logical or syntactical error.

when program generate exception then it's alter program execution so it is important to identified and dealt exception in proper way and write some exception handling code in program that handle the exceptional condition and make a program to run in smooth way.
exception in c++ are mainly two type

1)synchronous exception
2)asynchronous exception
Exception like division by zero, array index out of bounds comes under synchronous exception and interrupt like keyboard interrupt comes under asynchronous exception.

How to handle Exception in c++
C++ mainly use three keyword to handle exception those are:
1)try
2)catch
3)throw
try block used to white statement that can generate exception and catch block used to handle or take appropriate action against try block generated exception. whenever try block find any exception then that used throw keyword to throw exception. catch block comes immediately after try block.
general form(definition block) of try and catch block are:
try
{

throw exception; //block of statement which detect and throw an exception

}
catch(exception type)//type of exception that catch block handles
{

//block of statements that handle exception

}


Example of Exception Handling in C++

Division by zero exception handling example
#include
int main()
{
int x,y;
cout <<"please enter value of x and y \n";
cin >> x;
cin >> y;
try
{

if(y!=0)
{
cout <<"Result of division(x/y)="<<x/y<<"\n";
}
else
{
//block of statement which detect and throw an exception
throw (y); //throws int object
}
}
catch(int i)
{

//block of statements that handle exception

cout <<"Exception caught : y="<<y<<"\n";

}

cout<<"Program Executed successfully";
return 0;
}

program detect and handle division by zero exception if user enter value of y=0 then try block generate exception and throw it to catch block where catch block show proper message othewise program execute smoothly and show output as division of x/y.


Multiple catch block in Exception handling

If program detect and throw more then one type of exception then we must provide multiple catch block to handle different type of exception. c++ support multiple catch statement for one try block. we can write multiple catch statement in one program. we can write multiple catch block like:
try
{

}
catch(type1 arg)
{

}
catch(type2 arg)
{

}
catch(type3 arg)
{

}
catch(type4 arg)
{

}


Rethrowing Exception in C++

Rethrowing exception means if exception handler decide to rethrowing exception without handling or processing it then handler can do that with invoking throw keyword without any argument.

Example of rethrowing exception
#include
int main()
{
try
{

rethrow1();

}
catch(int i)
{
cout <<" Rethrow Exception caught here: y="<<y<<"\n";

}
cout<<"Program Executed successfully with rethrow exception";
return 0;
}

void rethrow1(){
int x,y;
cout <<"please enter value of x and y \n";
cin >> x;
cin >> y;
try
{

if(y!=0)
{
cout <<"Result of division(x/y)="<<x/y<<"\n";
}
else
{
//block of statement which detect and throw an exception
throw (y); //throws int object
}
}
catch(int i)
{

throw; //Rethrow the exception

}

}