C++ Exception Handling

It's very rare that a large program or software works correctly the first time. It might have errors.

The two most common types of errors are:

  • Logical errors
  • Syntactic errors

Programmers can debug these errors by exhausting debugging and testing procedures. But programmers often come across some peculiar problems in addition logic or syntax errors. These types of errors are known as exceptions. Exceptions are run-time anomalies or unusual logical conditions that may come up while executing the C ++ program. In this chapter, you will learn about these anomalies and how to handle these anomalies within a C++ program.

What is exception handling?

Exceptions allow a method to react to exceptional circumstances and errors (like runtime errors) within programs by transferring control to special functions called handlers. For catching exceptions, a portion of code is placed under exception inspection. Exception handling was not a part of the original C++. It is a new feature that ANSI C++ included in it. Now almost all C++ compilers support this feature. Exception handling technology offers a securely integrated approach to avoid the unusual predictable problems that arise while executing a program.

There are two types of exceptions:

  1. Synchronous exceptions
  2. Asynchronous exceptions

Errors such as: out of range index and overflow fall under the category of synchronous type exceptions. Those errors that are caused by events beyond the control of the program are called asynchronous exceptions. The main motive of the exceptional handling concept is to provide a means to detect and report an exception so that appropriate action can be taken. This mechanism needs a separate error handling code that performs the following tasks:

  • Find and hit the problem (exception)
  • Inform that the error has occurred (throw exception)
  • Receive the error information (Catch the exception)
  • Take corrective actions (handle exception)

The error handling mechanism basically consists of two parts. These are:

  1. To detect errors
  2. To throw exceptions and then take appropriate actions

Exception handling in C++ is built on three keywords: try, catch, and throw.

  • try
  • throw: A program throws an exception when a problem is detected which is done using a keyword "throw".
  • catch: A program catches an exception with an exception handler where programmers want to handle the anomaly. The keyword catch is used for catching exceptions.

The Catch blocks catching exceptions must immediately follow the try block that throws an exception.

The general form of these two blocks is as follows:

Syntax:
try
{
    throw exception;
}

catch(type arg)
{
    //some code
}

If the try block throws an exception then program control leaves the block and enters into the catch statement of the catch block. If the type of object thrown matches the argument type in the catch statement, the catch block is executed for handling the exception. Divided-by-zero is a common form of exception generally occurred in arithmetic based programs.

A Simple Program of Exception Handling in C++

Example:
#include<iostream>
using namespace std;

int main()
{
    try {
        throw 6;
    }

    catch (int a) {
        cout << "An exception occurred!" << endl;
        cout << "Exception number is: " << a << endl;
    }
}

The following example shows handling of division by zero exception.

Example:
#include<iostream>
using namespace std;

double division(int var1, int var2)
{
    if (var2 == 0) {
        throw "Division by Zero.";
    }
    return (var1 / var2);
}

int main()
{
    int a = 30;
    int b = 0;
    double d = 0;

    try {
        d = division(a, b);
        cout << d << endl;
    }
    catch (const char* error) {
        cout << error << endl;
    }

    return 0;
}
Example:
Division by zero.

Advantages of Exception Handling

  1. Programmers can deal with them at some level within the program
  2. If an error can't be dealt with at one level, then it will automatically be shown at the next level, where it can be dealt with.

Scroll Back to Top