C++ Exception Handling4 min read

An exception is an event or an error that arises during the execution of a program i.e. during runtime. The occurrence of an exception in a program disrupts the normal flow of instructions.

So to handle such errors, we use exception handlers. If we do not handle the exception properly then the program may terminate. We can catch an exception through exception handling and our program will not completely terminate.

The mainĀ advantageĀ of exception handling is to ensure that even when an exception occurs, the program’s flow doesn’t break.

C++ Exception Handling Keywords

C++ provides three keywords to perform exception handling. They are:

  • try: The try block identifies the block of code for which the exception will be activated. It is followed by a catch block.
  • catch: The catch indicates the cathching of an exception where you want to catch the error accuring in the program.
  • throw: throw keyword is used to throw an exception when a problem arises in a program.

try/catch in C++

Assume that the block of code will cause an error and you want to catch that exception. The try and catch block will help you to do that. You place a code inside the try block which you think causing the problem and catch the statement to catch the exception.

The syntax for try and catch:

We can place a multiple catch statement on different types of exceptions that may arise by try block code.

Example: C++ try/catch

Use of try, catch and throw keywords in a C++ program.

Output:

Division by Zero attempted.


C++ Standard Exceptions

C++ provides a list of standard exceptions defined in <exception> class that we can use in a program.

c++ exception handling

These exceptions and descriptions are listed in the table below.

ExceptionDescription
std::exceptionThis is an exception and the parent class of all standard C++ exceptions.
std::bad_allocThis can be thrown by a keyword new.
std::bad_cast This can be thrown by dynamic_cast.
std::bad_exceptionA useful device for handling unexpected exceptions in C++ programs.
std::bad_typeidAn exception thrown by typeid.
std::logic_errorThis exception is theoretically detectable by reading code.
std::domain_errorThis is an exception thrown after using a mathematically invalid domain.
std::invalid_argumentThis exception is thrown for using invalid arguments.
std::length_errorThis exception is thrown after creating a big std::string.
std::out_of_rangeThrown by at method.
std::runtime_errorThis is an exception that cannot be detected via reading the code.
std::overflow_errorThis exception is thrown after the occurrence of a mathematical overflow.
std::range_errorThis exception is thrown when you attempt to store an out-of-range value.
std::underflow_errorThis exception is thrown after the occurrence of mathematical underflow.

User-Defined Exceptions

The C++ std::exception class allows us to define objects that can be thrown as exceptions. This new exception can be defined by overriding and inheriting the exception class functionality. This class has been defined in the <exception> header.

Example: user-defined exception in c++

Output:

Occurance of new exception


MORE

Find a pair with the Given Sum in an Array in Java

In this tutorial, we will write a java program to find the sum pair in an array. Before that, you may go through the following …
Read More

C++ Program to display Pascal Triangle

In this tutorial, we will write a C++ program to display pascal’s triangle. Before that, you may go through the following topic in C++. for …
Read More

C++ Program to Calculate Sum of Geometric Progression

In this tutorial, we will learn and write a program to find the sum of GP series in C++. Before that, you may go through …
Read More

C++ Program to find the Transpose of a Matrix

In this tutorial, we will learn and write a program to find the transpose of matric in C++. Before that, you may go through the …
Read More

How to Split a String in C++

Let us learn how to split string in C++ programming language. We will look at a few ways to split a string. The splitting of …
Read More

C++ Program to calculate Average of Numbers using Arrays

In this tutorial, we will learn and write a C++ program to find the average of numbers using an array. To understand the coding, you …
Read More