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 the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …
Read More

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More