What is Exception Handling in C++?

What is Exception Handling in C++? (try and catch block)
While doing
programming we come many across errors. Most commonly there are two types of
errors, logical errors and syntactical errors. The logical errors occur due to
the poor understanding of problem and on the other hand syntactical errors
arise due to poor understanding of language.

Many times we face
some strange or odd problems other than these two types of errors, these are
known as exceptions. Exceptions are the run time unusual conditions that a program may encounter while execution. It might include conditions such as
division by zero, out-of-range index, running out of memory, etc.

So we can say that
the process of handling these types of exceptional conditions is known as
exception handling. This is a new feature added to ANSI C++. Today, almost all
compilers support this feature.

The exception
handling process includes the following four steps:

1. Find the problem
(Hit the exception)

2. Inform that an
error has occurred (Throw the exception)

3. Receive the error
information (Catch the exception)

4. Take corrective
actions (Handle the exception)

Exception Handling Mechanism

C++ exception
handling mechanism consists of three keywords, try, throw and catch.

try: The keyword try is used to define a block of statements
which may produce exceptions and this block is known as try block.

throw: When an exception is detected, it is thrown using a throw statement in the try block.

catch: This block catches the exception thrown
by throw statement in the try block and handles it appropriately. The catch block that catches an exception
must immediately come after the try block that throws the exception.

The structure of
these two blocks is shown below:

. . . . . .

. . . . . .

try

{

                . . . . . .

                throw exception                  //Block of
statements which detects and throws an exception

                . . . . . .

                . . . . . .

}

catch(type arg)

{

                . . . . . .                             //Block
of statements that handles the exception

                . . . . . .

}

. . . . . .

. . . . . .
When the try block throws an exception, the
program control leaves the try block
and enters the catch statement of
the catch block. Here exception is nothing but an object used to transmit the
information about a problem. If the type of object thrown matches the arg type
of the catch statement then catch
block is executed for handling the exception. If they do not match, the program
is aborted with the help of abort() function which is invoked by default. When no
exception is detected and thrown, the control goes to the statement immediately
after the catch block or we can say catch block is skipped.

A program is given
below that will explain you that how actually exception handling is done in C++.

#include<iostream>

using namespace std;

int main()

{

   
int x,y,z;

   
cout<<“Enter the values of x and y:”;

   
cin>>x>>y;

   
z=x-y;

   
try

   
{

       
if(z!=0)

       
{

           
cout<<“Result(x/z)=”<<x/z;

       
}

       
else                                             //division by zero exception

       
{

            throw(z);                                 //throws int
object

       
}

   
}

   
catch(int)                                        //catches
the exception

   
{

       
cout<<“Exception caught: z=”<<z;

   
}

   
return 0;

}
Outputs of the above program is shown below:
For First Run 
 

What is Exception Handling in C++?
For Second Run
What is Exception Handling in C++?
If you have any
doubts then feel free to ask by commenting below. Please do share if you liked
the article.

Source: E
Balagurusamy

5 thoughts on “What is Exception Handling in C++?”

Leave a Comment

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