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

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

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 …