×

Search anything:

Exception handling in C++

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Reading time: 20 minutes | Coding time: 5 minutes

Exceptions are runtime anomalies/ errors or unusual conditions that a program may encounter during its execution. It is important to handle these situations to ensure that the program does not show undefined behaviour during runtime like terminating execution in between.

C++ provides several ways to handle exceptions. We can do so by handling exceptions using custom code which will use the following keywords:

  • try
  • throw
  • catch

In addition to that, C++ provides custom exceptions (predefined) which we can use and it has std::cerr which is a stream for error outputs.

try

The keyword try is used to preface the code you want to test i.e all the code to be tested for exceptions is put inside the try block

For example

try {
    /* code to be checked */
}

throw

An exception is thrown by using the throw keyword from inside the try block.

try {
    throw exception;
}

A throw expression triggers that an exception condition (error) has occurred in a try block. You can use an object of any type as the operand of a throw expression i.e exception can be of any object type or variable. Typically, this object is used to communicate information about the error.

catch

So you threw an exception now what, you want it to be handled right ??
That's where catch comes to play. A catch is just a code block similar to try

catch(type obj) {
    /* code to handle exception */
}

try_catch_block

To handle exceptions that may be thrown, implement one or more catch blocks immediately following a try block. Each catch block specifies the type of exception it can handle i.e if you threw more than one type of exception you would have to implement multiple catch blocks to handle them.

catch(type obj1) {
    /* code to handle exception of type 1*/
}
catch(type obj2) {
    /* code to handle exception of type 2*/
}
catch(type ob3) {
    /* code to handle exception of type 3*/
}

If the type of object thrown by the try block matches the any obj type in any catch statement than that catch block is executed.
For handling exceptions of any type other than thrown by you

Ellipses (...) is used in the catch block arguments.

catch(...) {
    /* Catches any kind of exception */
}

So far the code structure looks like:

try {
    throw obj1;
    throw obj3;
}

catch(type obj1) {
    std::cout<<"Caught Exception of type obj1 ";
}
catch(type obj3) {
    std::cout<<"Caught Exception of type obj3 ";
}
catch(...){
    std::cout<<"Last catch block ";
}

Note that once a exception is thrown it is matched with the catch blocks one by one if a match is found than that block is executed else it continues on checking for a match.

If no catch block matches, the last catch(...) block is executed.

Example

Below is a simple implementation of the things we learned so far.
The program does division of two numbers and if the Denominator is 0 it throws an exception.

#include <iostream>

int main(){
    int a, b, c;
    std::cout<<"\nEnter Numerator & Denominator : ";
    std::cin >> a >> b;
    // try block to handle exception handling
    try {
        if(b == 0){
            throw "Division by Zero Error";     // throw custom exception
        }
        c = a/b;
        std::cout<<"\nDivision Result = " << c; 
    }
    catch(char* exception){ // catches exception
        std::cout << exception;
    }
    return 0;
}

The above program prints

Enter Numerator & Denominator : 2 0
Division by Zero Error

Custom Exceptions

We can define our own exceptions in C++ by inheriting from the class Exception and overriding its what() function.

Here is a sample code

#include <iostream>
#include <exception>
using namespace std;
 
class MyException : public exception {
 public:
    const char * what () {
      return "Custom C++ Exception";
   }
};
 
int main() {
   try {
      throw MyException();
   }catch(MyException e) {
      cout << "MyException caught" <<endl;
      cout << e.what() <<endl;
   }catch(exception e) {
      //Other errors
   }
   return 0;
}

The above program would print

MyException caught
Custom C++ Exception

what() is a predefined function in the <exception> header which just returns an explanatory string regarding the exception.

std::cerr

Just like we use std::cout to write data to standard output stream and std::cin for input .

std::cerr is used to write data to standard error stream.

The 'c' in the name refers to "character";
cerr means "character error (stream)"

For example

#include <iostream>
int main(){
  std::cerr << "Error Stream !";
}
Exception handling in C++
Share this