Exception Handling C++
Exception Handling C++
EXCEPTIONS
Exceptions are run time anomalies or unusual conditions that a program
Conditions such as
Division by zero
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)
catch block
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…)
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
written as catch(…)
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
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";
}
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