EXCEPTION HANDLING IN C++
EXCEPTIONS
Exceptions are run time anomalies or unusual conditions that a program
may encounter during execution
Conditions such as
Division by zero
Access to an array outside of its bounds
Running out of memory
Running out of disk space
EXCEPTION HANDLING
It was not a part of original C++
It is a new feature added to ANSI C++
Exceptions are of 2 kinds
Synchronous Exception
Out of rage
Over flow
Asynchronous Exception
Error that are caused by causes beyond the control of the program
Keyboard interrupts
EXCEPTION HANDLING (CONT…)
Exception handling mechanism
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)
EXCEPTION HANDLING MECHANISM
It is basically build upon three keywords
try
try block
throw Detects and
throw an exception
catch
Exception Object
catch block
Catch and handle
the exception
EXCEPTION HANDLING MECHANISM (CONT…)
The keyword try is used to preface a block of statements which may generate
exceptions
When an exception is detected, it is thrown using a throw statement in the try block
A catch block defined by the keyword catch catches the exception and handles it
appropriately
The catch block that catches an exception must immediately follow the try block
that throws the exception
More than one catch statement can be associated with a try block
EXCEPTION HANDLING MECHANISM (CONT…)
try
{
… // Block of statements which may cause exceptions and // throws an exception
throw exception;
…
}
catch(type arg)
{
…
…
…
}
CATCHING MECHANISM
try
{
throw exception;
}
catch(type1 arg)
{
// catch block 1
}
catch(type2 arg)
{
// catch block 2
}
…
…
catch(typeN arg)
{
// catch block N
}
EXCEPTION HANDLING MECHANISM (CONT…)
Exceptions are objects used to transmit information about a problem
When an exception is thrown, the exception handlers are searched in order
for a match
If several catch statement matches the type of an exception the first handler
that matches the exception type is executed
If the type of the object thrown matches the arg type in one of the catch
statements, the catch block is executed
If they do not match, the program is aborted
CATCHING MECHANISM
There is a special catch block called
catch all block
written as catch(…)
Can be used to catch all types of exceptions
RETHROWING AN EXCEPTION
A handler may decide to rethrow the exception caught without processing it.
In such a case we have to 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 the enclosing try block
Class exception
The standard C++ base class for all exceptions
Provides derived classes with virtual function what
Returns the exception’s stored error message
Standard Library exception classes
Example 1
int main()
{
int x = -1;
cout << "Before try \n";
try
{ Output
cout << "Inside try \n";
if (x < 0)
{ terminate called after throwing an instance of
throw x;
} 'char‘
}
catch (double x )
{ This application has requested the Runtime to
cout << "Exception Caught \n";
terminate it in an unusual way. Please contact the
}
application's support team for more information.
cout << "After catch (Will be executed) \n";
return 0;
Example 2
int main()
{
int x = -1;
cout << "Before try \n";
try
{
cout << "Inside try \n";
Output
if (x < 0)
{
throw x;
Before try
cout << "After throw (Never executed) \n"; Inside try
}
} Exception Caught
catch (int x )
After catch (Will be executed)
{
cout << "Exception Caught \n";
}
cout << "After catch (Will be executed) \n";
return 0;
Example 3
#include <iostream>
using namespace std;
int main()
{
try Output
{
throw 10;
} Exception caught in the second catch block
catch (double y)
{
cout << “Exception caught in the first catch block" << y;
}
catch (int z)
{
cout << "Exception caught in the second catch block\n";
}
return 0;
}
Example 4
#include <iostream>
using namespace std;
int main()
{
try Output
{
throw 10;
} Exception caught in the default catch
catch (double y)
block
{
cout << “Exception caught in the first catch block" << y;
}
catch (…)
{
cout << "Exception caught in the default catch block\n";
}
return 0;
}
Example 5
#include <iostream>
using namespace std;
int main()
{
try
{ Output
try
{
throw 20; Handle Partially
}
catch (int n) Handle remaining
{
cout << "\nHandle Partially ";
throw; // Re-throwing an exception
}
}
catch (int n)
{
cout << "\nHandle remaining ";
}
return 0;
Example 5
#include <iostream> int main()
using namespace std; {
try
class Test {
Test t1;
{
throw 10;
public: }
Test() catch (int i)
{ {
cout << "Constructor of Test executed " << endl; cout << "Caught " << i << endl;
}
} }
~Test()
{
cout << "Destructor of Test executed " << endl;
Output
}
};
Constructor of Test executed
Destructor of Test executed