Exception Handling
Exception Handling
Exception Handling
try block
The code which can throw any exception is kept inside(or enclosed in) atry block.
Then, when the code will lead to any error, that error/exception will get caught
inside the catch block.
www.vectorindia.org 1
Exception Handling
catch block
catch block is intended to catch the error and handle the exception condition. We
can have multiple catch blocks to handle different types of exception and perform
different actions when the exceptions occur. For example, we can display
descriptive messages to explain why any particular excpetion occured.
throw statement
It is used to throw exceptions to exception handler i.e. it is used to communicate
information about error. A throw expression accepts one parameter and that
parameter is passed to handler.
throw statement is used when we explicitly want an exception to occur, then we
can use throw statement to throw or generate that exception.
The above program will not run, and will show floating point exception on
screen, because we are trying to divide a number with 0, which is not possible.
How to handle this situation? We can handle such situations using exception
handling and can inform the user that you cannot divide a number by zero, by
displaying a message.
www.vectorindia.org 2
Exception Handling
#include <iostream>
In the code above, we are checking the divisor, if it is zero, we are throwing an
exception message, then the catch block catches that exception and prints the
message.
Doing so, the user will never know that our program failed at runtime, he/she will
only see the message "Division by zero not possible".
This is gracefully handling the exception condition which is why exception
handling is used.
www.vectorindia.org 3
Exception Handling
int main()
{
int x[3] = {-1,2};
for(int i=0; i<2; i++)
{
int ex = x[i];
try
{
if (ex > 0)
// throwing numeric value as exception
throw ex;
else
// throwing a character as exception
throw 'e';
}
catch (int ex) // to catch numeric exceptions
{
cout << "Integer exception\n";
}
catch (char ex) // to catch character/string exceptions
{
cout << "Character exception\n";
}
}
}
Output:
#include <iostream>
using namespace std;
www.vectorindia.org 4
Exception Handling
int main()
{
int x[3] = {-1,2};
for(int i=0; i<2; i++)
{
int ex=x[i];
try
{
if (ex > 0)
throw ex;
else
throw 'ex';
}
// generalised catch block
catch (...)
{
cout << "Special exception\n";
}
}
return 0;
}
There are some standard exceptions in C++ under <exception> which we can use
in our programs. They are arranged in a parent-child class hierarchy which is
depicted below:
std::exception - Parent class of all the standard C++ exceptions.
logic_error - Exception happens in the internal logical of a program.
domain_error - Exception due to use of invalid domain.
invalid argument - Exception due to invalid argument.
out_of_range - Exception due to out of range i.e. size requirement
exceeds allocation.
length_error - Exception due to length error.
runtime_error - Exception happens during runtime.
range_error - Exception due to range errors in internal computations.
overflow_error - Exception due to arithmetic overflow errors.
www.vectorindia.org 5
Exception Handling
www.vectorindia.org 6