Exception Handling in C++

What is Exception?

  • Exception is a problem that occurs during the execution of a program.
  • It is a response to exceptional circumstances like runtime errors while a program is running.

What is Exception Handling?

  • An exception transfers the control to special functions called Handlers.
  • Exception handling makes it easy to separate the error handling code.
  • It provides a way to transfer control from one part of a program to another.
  • Exception handling makes code readable and maintainable. It separates the code to the normal flow.
  • Function can handle or specify any exceptions they choose.
Following are the three specialized keywords where exception handling is built.
1. throw
2. try
3. catch

1. throw

  • Using throw statement, an exception can be thrown anywhere within a code block.
  • It is used to list the exceptions that a function throws, but doesn't handle itself.
Following example shows throwing an exception when dividing by zero condition occurs.

float div(int x, int y)
{
    if(y==0)
    {
         throw ”Divide by zero condition!!!”;
    }
    return (x/y);
}

2. try

  • Try represents a block of code that can throw an exception.
  • It identifies a block of code which particular exceptions will be activated.
  • Try block followed by one or more catch blocks.
Syntax:

try
{
    //Code
}
catch(ExceptionName e1)
{
    //Catch block
}
catch(ExceptionName e2)
{
    //Catch block
}
catch (ExceptionName e3)
{
    //Catch block
}

3. Catch

  • Catch represents a block of code executed when a particular exception is thrown.
  • It follows the try block which catches any exception.
Syntax:

try
{
    //code
}
catch(ExceptionName e)
{
    //code to handle exception
}


In the above syntax, code will catch an exception of ExceptionName type.

Example: Program demonstrating flow of execution of Exception

#include <iostream>
using namespace std;
int main()
{
   int a = -2;
   cout << "Before Try Block"<<endl;
   try
   {
      cout << "Inside Try Block"<<endl;
      if (a < 0)
      {
         throw a;
         cout << "After Throw Exception (Never executed)"<<endl;
      }
   }
   catch (int a )
   {
      cout << "Exception Caught \n";
   }
   cout << "Executed After Catch Exception"<<endl;
   return 0;
}


Output:
Before Try Block
Inside Try Block
Exception Caught
Executed After Catch Exception

You can define your own exceptions. Following example shows how to implement your own exception.

Example: Program demonstrating developer can create their own exceptions

#include <iostream>
#include <exception>
using namespace std;

struct MyException : public exception
{
   const char *test () const throw ()
   {
      return "New Exception";
   }
};
int main()
{
   try
   {
      throw MyException();
   }
   catch(MyException &e)
   {
      cout<< "MyException caught"<<endl;
      cout<< e.test()<<endl;
   }
}


Output:
MyException caught
New Exception

In the above program, test() is a public method provided by exception class. It returns the cause of an exception.