Exception Handling in C++

Errors can be broadly categorized into two types. We will discuss them one by one.

  1. Compile Time Errors
  2. Run Time Errors

Compile Time Errors – Errors caught during compiled time is called Compile time errors. Compile time errors include library reference, syntax error or incorrect class import.

Run Time Errors - They are also known as exceptions. An exception caught during run time creates serious issues.

Errors hinder normal execution of program. Exception handling is the process of handling errors and exceptions in such a way that they do not hinder normal execution of the system. For example, User divides a number by zero, this will compile successfully but an exception or run time error will occur due to which our applications will be crashed. In order to avoid this we'll introduce exception handling technics in our code.

In C++, Error handling is done by three keywords:-

  • Try
  • Catch
  • Throw

Syntax:

Try
{
//code
throw parameter;
}
catch(exceptionname ex)
{
//code to handle exception
}
Try

Try block is intended to throw exceptions, which is followed by catch blocks. Only one try block.

Catch

Catch block is intended to catch the error and handle the exception condition. We can have multiple catch blocks.

Throw

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.


Example of Exception

Below program compiles successful but the program fails during run time.

#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int a=10,b=0,c;
c=a/b;
return 0;
}

Implementation of try-catch, throw statement

Below program contains single catch statement to handle errors.

#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int a=10,b=0,c;
try //try block activates exception handling
{
if(b==0)
throw "Division by zero not possible";//throws exception
c=a/b;
}
catch(char* ex)//catches exception
{
cout<<ex;
}
getch();
return 0;
}

Output:

0

Example for multiple catch statement

Below program contains multiple catch statements to handle exception.

#include <iostream>
#include<conio.h>
using namespace std;
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';
} catch (int ex) {
cout << " Integer exception;
} catch (char ex) {
cout << " Character exception” ;
}
}
getch();

Output:

Integer exception Character exception

Example for generalized catch statement

Below program contains generalized catch statement to catch uncaught errors. Catch(…) takes care of all type of exceptions.

#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int x[3]={-1,2,};
for(int i=0;i<2;i++)
{
int ex=x[i];
try {
if (ex > 0)
throw x;
else
throw 'ex';
} catch (int ex) {
cout << " Integer exception" ;
} catch (char ex) {
cout << " Character exception" ;
}catch (...) {
cout << "Special exception";
}
}
getch();
return 0;
}

Output:

Integer exception Special exception

Standard Exceptions in C++

There are 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.
    • underflow_error - Exception due to arithmetic underflow errors
  • bad_alloc - Exception happens when memory allocation with new() fails.
  • bad_cast - Exception happens when dynamic cast fails.
  • bad_exception - Exception is specially designed to be listed in the dynamic-exception-specifier.
  • bad_typeid - Exception thrown by typeid.