Compiled By:
Sushant Paudel
Exceptions
Introduction
Exceptions are runtime anomalies or unusual conditions that a program may
encounter while executing. Exceptions might include conditions such as division by
zero, access to an array outside of its bounds, running out of memory or disk
space, not being able to open a file, trying to initialize an object to an impossible
value etc. When a program encounters an exceptional condition, it is important
that it is identified and dealt with effectively. C++ provides built-in language
features to detect and handle exceptions, which are basically runtime errors.
The purpose of the exception handling mechanism is to provide means to detect
and report an “exceptional circumstance” so that appropriate action can be taken.
The mechanism suggests a separate error handling code that performs the
following tasks:
Find the problem (Hit the exception).
Inform that an error has occurred (Throw the exception).
Receive the error information (Catch the exception).
Take corrective action (Handle the exception).
The error handling code basically consists to two segments, one to detect errors
and to throw exceptions, and the other to catch the exceptions and to take
appropriate actions.
Exception Handling Mechanism
Exception handling mechanism in C++ is basically built upon three keywords:
try,throw, and catch. The keyword try is used to surround a block of statements,
which may generate exceptions. This block of statements is known as try block.
When an exception is detected, it is thrown using the throw statement situated
either in the try block or in functions that are invoked from within the try block.
This is called throwing an exception and the point at which the throw is executed is
called the throw point.
A catch block defined by the keyword catch catches the exception thrown by the
throw statement and handles it appropriately. This block is also called exception
handler. The catch block that catches an exception must immediately follow the try
block that throws an exception.
The figure below shows the exception handling mechanism if try block throws an
exception. The general form in this case is:
The figure below shows the exception handling mechanism if function invoked by
try block throws an exception.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,b,c;
cout<<"enter the two numbers"<<endl;
cin>>a>>b;
try{
if(b==0)
throw"division by zero not possible";
c=a/b;
cout<<"result:"<<c;
}
catch(const char* ex)
{
cout<<ex;
}
}
#include<iostream> cout<<"Divide by zero exception: b = "<<i;
#include<conio.h> }
using namespace std; cout<<"\nEND";
int main() getch();
{ }
int a, b;
cout<<"Enter values of a & b:\n";
cin>>a>>b;
try
{
if(b == 0)
throw b;
else
cout<<"Result = "<<(float)a/b;
}
catch(int i)
{
#include <iostream> }
using namespace std; else
int main () {
{ throw (y);
int x = 50; }
int y = 0; } catch (int m)
double z = 0; {
try { cout<<"division by"<<m<<"is not possible"<< endl;
if(y!=0) }
{ return 0;
int z=x/y; }
cout << z << endl;
Function invoked by try block throwing exception try
#include<iostream> {
#include<conio.h> divide(a, b);
using namespace std; }
void divide(int a, int b) catch(int i)
{ {
if(b == 0) cout<<"Divide by zero exception: b ="<<i;
throw b; }
else cout<<"\nEND";
cout<<"Result = "<<(float)a/b; getch();
} }
int main()
{
int a, b;
cout<<"Enter values of a & b:\n";
cin>>a>>b;
Multiple Catch Statements:
It is possible that a program segment has more than one condition to throw an exception. In such cases, we can associate more than
one catch statement with a try as shown below:
try
{
Try block
}
catch(type1 arg)
{
Catch block1
}
catch(type2 arg)
{
Catch block 2
}
……… ………
}
catch(typeN arg)
{
Catch block N
}
#include<iostream> }
#include<conio.h> catch(double d)
using namespace std; {
void test(int x) cout<<"Caught a double";
{ }
try }
{ int main()
if(x == 0) throw x; {
if(x == 1) throw 1.0; test(0);
} test(1);
catch(int m) test(2);
{ getch();
cout<<"Caught an integer\n"; }
Catch All Exceptions:
If we want to catch all possible types of exceptions in a single catch block, we use
catch in the following way: catch(…)
{
Statements for processing all exceptions
}
#include<iostream> }
#include<conio.h> }
using namespace std; int main()
void test(int x) {
{ test(0);
try test(1);
{ test(2);
if(x == 0) throw x; getch();
if(x == 1) throw 1.0; }
}
catch(...)
{
cout<<"Caught an all exception\n";
Rethrowing an Exception
A handler may decide to rethrow the exception caught without processing it. In such
situations, we may simply invoke throw without any arguments as shown below:
throw; This causes the current exception to be thrown to the next enclosing
try/catch sequence and is caught by a catch statement listed after that enclosing try
block.
//Rethrowing an Exception int main()
#include<iostream> {
#include<conio.h> int a, b;
using namespace std; cout<<"Enter values of a & b:\n";
void divide(int a, int b) cin>>a>>b;
{ try
try {
{ divide(a, b);
if(b==0) throw b; }
else catch(int i)
cout<<"Result = "<<(float)a/b; {
} cout<<"Divide by zero exception: b = "<<i;
catch(int) }
{ cout<<"\nEND";
throw; getch();
} }
}
Specifying Exceptions
It is also possible to restrict a function to throw only certain specified exceptions. This is achieved by
adding a throw list clause to the function definition. The general form is as follows:
type function(arg-list) throw (type-list)
{
Function body
}
The type-list specifies the type of exceptions that may be thrown. Throwing any other type of
exception will cause abnormal program termination. If we wish to prevent a function from throwing
any exception, we may do so by making the type-list empty. Hence the specification in this case will
be
type function(arg-list) throw ()
{
Function body
}
Note:
A function can only be restricted in what types of exception it throws back to the try block that
called it. The restriction applies only when throwing an exception out of the function (and not within
the function).
//Specifying Exceptions {
#include<iostream> cout<<"Caught an integer\n";
#include<conio.h> }
using namespace std; catch (double d)
void test(int x) throw (int,double) {
{ cout<<"Caught a double";
if(x == 0) throw x; }
if(x == 1) throw 1.0; getch();
} }
int main()
{
try
{
test(1);
}
catch(int m)