Exception Handling In C++

Check out whether your C++ compiler supports this feature (the older C++ compilers will not support exception handling).
 
This feature is used for dealing with certain specific situations (these situations are not supposed to occur and if you don’t provide some way of dealing with them then your program could simply freeze when the conditions really occur).
 
To implement exception handling, C++ provides us with three keywords:
 
Try
Catch
Throw
 
How does it work? First you try (execute) the coding and in case some special situation is encountered, then the program will throw an exception. The exception that is thrown, will be caught by catch.
 
int main ( )
{
float a,b;
try
{
cout<<“\nEnter a number : “;
cin>>a;
cout<<“\nEnter the denominator : “;
cin>>b;
 
if (b = =0)
{
throw= 0;
}
cout<<“\nResult of division : “<<
}
 
catch( int i )
{
cout<<“\nError – You entered : “<< i ;
}
return 0;
}
 
The output if you enter two non-zero numbers will be:
 
Enter a number : 5
Enter the denominator : 0
Error- you entered : 0
 
The coding should be easy to comprehend. You should note that once the program throws something, it will not execute what follows it within the try block.
 
It will go directly to the catch block and execute the underlying statements. The catch block that you write should immediately follow after the try block.
 
Even if you add one display statement between the try and catch blocks it will lead to errors. Can We have more than one catch??? Yes but each catch should catch a different exception (meaning that the argument types for each one should be different).
 
You can write one catch block to catch integers, one to catch strings etc. Suppose you want to catch everything that is thrown, there is a simple way to do it. Just use:
 
catch ( )
{
//body of catch
}
 
The 3 consecutive dots imply that this catch block will catch anything that is thrown.
 
Uncaught Exceptions
 
This is an interesting topic. What will happen if We throw a string but We have programmed for catching only integers? This is what is called as an uncaught exception and if you try it out, your compiler will display the message:
 
Abnormal Program Termination
 
Actually, what the compiler does is that it executes a terminate ( ) function. The terminate ( ) function will call the abort ( ) function.
 
Usually, if there is some problem with the exception handling, then the compiler would either go to terminate ( ) function or it would invoke the unexpected ( ) function.
 
The unexpected ( ) function will call the terminate ( ) function. Hence, ultimately the program will abort through the abort ( ) function.
 
You can create your own version of the terminate function (in other words, you can set the terminate function) using:
 
set_terminate(your-function);
 
For using this, you would need to make use of the header file.
 
void myterminate( )
{
cout<<“\nMy Terminate Function”;
abort( )
}
int main ( )
{
float a, b;
try
{
set_terminate(myterminate);
cout<<“\nEnter a number : “;
cin>>a;
cout<<“\nEnter the denominator : “;
cin >>b;
if ( b= = 0)
{
throw “zero” ;
}
cout<<“\nResult of division : “<<a/b ;
}
 
catch( int i)
{
cout<<“\nError – You entered : “<< i ;
}
return 0 ;
}
 
The output would be (when you enter the second number as 0):
 
Enter a number: 5
 
Enter the denominator: 0
 
My Terminate Function
 
Abnormal program termination
 
Thus you can see that the terminate ( ) function has been modified and even in our terminate ( ) function we make use of the abort ( ) function.
 
The idea is that you do not want the program execution to continue once the compiler encounters some problem with the exception handling.
 
The also defines some standard exceptions that are thrown. For example:
 
bad_alloc- thrown if some problem is encountered using ‘new’
 
bad_exception- thrown when exception doesn’t match any catch
Scroll to Top