Exception Handling in C++

Exception Handling in C++


Exception act as an event that takes place while executing any program, which throws the normal flow of instructions of the program.

Suppose we are writing one program where we forget to declare one variable and trying to use that variable. Our system show error that undefined symbol, this is possible by the predefined program. whenever this error occurs, it automatically displays this error on the screen. Now the user is easy to understand what is the error and also able to do the correction. This same thing is provided for C++ compiler through exception handling.

The conversion of system error message into the user-friendly message is known as exception handling.

Generally, errors are of three types compile, logical and execution time errors.

Compile time errors can be syntax, library references, Logical errors can be due to the wrong logic is written on the program. Runtime errors are also called as exception errors. Error handling can be done. Exception handling take place during runtime error.


When anyone tries to divide no of 0 i.e. y/0. It compiles successfully but generate runtime error.

Advantage of Exception Handling

It permits users to handle runtime errors and maintain the normal flow of C++ programming language application.

Types of Exception


These are generated due to some unusual conditions such as Array index out of bound,



These exceptions are due to such events that are out of C++ program. Such as keyword

C++ exception handling mechanism is generated for handling synchronous exceptions. The main objective of the handling exception concept is to make available means for detecting and reporting an exception such that convenient action can be performed. For this, a separate error handling code is required that operate the following tasks:

a) Search and hit exceptions
Inform that the error has occurred (throw exception)
c) Catch exceptions in which error information can be received.
Handle exceptions through corrective actions

This block includes two blocks

Keywords used in Exception Handling

In C++ programming language, there are following three keywords used for operating Exception handling

* Try
* Catch
* Throw

In C++ programming language, an exception can be thrown by the throw keyword. When some possibilities of some exceptional condition are known in the program,  the exception throw. The throw expression is used for error related communication where one parameter is accepted by throw expression and then, it is then handover to the handler.

The main usage of the catch block is to catch the program's error and manage the exception condition. There can be multiple catch blocks in the C++ program.

The main usages of the Try block to throw exceptions that can further proceed through catch blocks. There can be only one try block.

Example of exception in C++ programming language

This following two program compiles but display runtimes error

#include <iostream> 
using namespace std;
int main()
int p=87,q=0,r;
return 0;


Before applying try
Inside the block of try
Exception in the program Caught
After the catch block

Following are some standard exceptions found in C++ that can be used under <exception> header.

Std::exception and many more

Best WordPress Hosting


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price

Leave a Reply

    Waiting for your comments