C++ Exception Handling

In this Tutorial we will understand the mechanism of Exception Handling in C++ Programming Language.

Exception Handling in C++

An exception is an unexpected problem that arises during the execution of a program. Exception handling mechanism provide a way to transfer control from one part of a program to another. This makes it easy to separate the error handling code from the code written to handle the actual functionality of the program.
C++ exception handling is built upon three keywords: try, catch, and throw.

  • try : A block of code which may cause an exception is typically placed inside the try block. It’s followed by one or more catch blocks. If an exception occurs, it is thrown from the try block.
  • catch : this block catches the exception thrown from the try block. Code to handle the exception is written inside this catch block.
  • throw : A program throws an exception when a problem shows up. This is done using a throw keyword.

Every try catch should have a corresponding catch block. A single try block can have multiple catch blocks.

Throwing Exceptions

Exceptions can be thrown anywhere within a code block using throw statements. The operand of the throw statements determines a type for the exception and can be any expression and the type of the result of the expression determines the type of exception thrown.

Following is an example of throwing an exception when dividing by zero condition occurs:

Catching Exceptions

The catch block following the try block catches any exception. You can specify what type of exception you want to catch and this is determined by the exception declaration that appears in parentheses following the keyword catch.

Above code will catch an exception of ExceptionName type. If you want to specify that a catch block should handle any type of exception that is thrown in a try block, you must put an ellipsis, , between the parentheses enclosing the exception declaration as follows:

Exception Handling Sample Program

Run Online

Division by zero condition!
Because we are raising an exception of type const char*, so while catching this exception, we have to use const char* in catch block. If we compile and run above code, this would produce the following result as given in the output box above.

C++ Standard Exceptions

exceptions in c++ programming

C++ provides a list of standard exceptions defined in <exception> which we can use in our programs.
Small Description of exceptions is given in the table as follows:

std::exceptionAn exception and parent class of all the standard C++ exceptions.
std::bad_allocThis can be thrown by new.
std::bad_castThis can be thrown by dynamic_cast.
std::bad_exceptionThis is useful device to handle unexpected exceptions in a C++ program
std::bad_typeidThis can be thrown by typeid.
std::logic_errorAn exception that theoretically can be detected by reading the code.
std::domain_errorThis is an exception thrown when a mathematically invalid domain is used
std::invalid_argumentThis is thrown due to invalid arguments.
std::length_errorThis is thrown when a too big std::string is created
std::out_of_rangeThis can be thrown by the at method from for example a std::vector and std::bitset<>::operator[]().
std::runtime_errorAn exception that theoretically can not be detected by reading the code.
std::overflow_errorThis is thrown if a mathematical overflow occurs.
std::range_errorThis is ocurred when you try to store a value which is out of range.
std::underflow_errorThis is thrown if a mathematical underflow occurs.
Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *