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

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More