Learn about the Mechanism of Exception Handling in C++

Exception Handling Mechanism in C++


To overcome the limitations of the conventional error handling mechanism, an exception

handling mechanism was introduced in C++ in 1989.C++ exception handling mechanism

not only guarantees the detection and handling of run-time errors but also provides a way

to separate the error handling code from the rest of the program. This makes the program

less complex, more readable and efficient, as the normal execution path is not interrupted

for checking errors. Moreover, the error handling routine (called exception handler) is

invoked automatically whenever an error occurs.

Learn More: 

An exception usually carries three types of information with it, namely, the type

of exception , the place where the exception has occurred and the context information such

as error message and other state information.


C++ exception handling mechanism mainly uses three keywords, try, throw and catch. A set

of statements that needs to be monitored for the exception is contained in the try block. The

try Black is surrounded by curly braces and prefixed by the keyword try. Whenever an exception

occurs within the try block, it is thrown using the throw statement. This passes the control

to the catch black which handles the exception appropriately. The throw statement and the

catch block are prefixed by the keywords throw and catch respectively.

The catch block is also called the exception handler and it must immediately follow the try block

that throws an exception. The syntax to define try-throw-catch block is-


……                                       //your program

try                                        //tryblock
{              …

throw exception  //throw statement

               …
}                                            //end of try block

catch (data_type parameter) // catch block
{

               …

}                                            //end of catch block


The exception thrown using the throw statement is the object that transmits the information

about the exception. This object is known as an exception object. Once an exception is thrown,

 the control is automatically transferred to the catch block and the try block is terminated. If

the data type of the exception object matches with the data type of the parameter specified in the

catch block, the catch block is executed. If an appropriate match is not found, the standard library

function terminate() is invoked, which by default calls the abort() function to terminate

the program abnormally. However, it is not necessary that every time the program is executed, 

an exception occurs. If an exception does not occur, the catch block is skipped and the 

control passes to the statement immediately following the catch block.


To understand the concept of exception handling mechanism, consider this example.

Example: A program to demonstrate the exception handling mechanism

#include<iostream>
using namespace std;
int main()
{
int a;

cout<<"Enter an ASCII value”;

cin>>a;

try          //try block
{

if (a>31 && a<= 255)
cout<<” the equivalent character is”<<char(a);
else throw(a);     //throwing an exception

}
catch(int i)           //catch block accepting int argument
{

cout<<"Exception-Non-printable character”;

}

return 0;
}


The output of the program is

First run

Enter an ASCII value: 45
The equivalent character is: -

Second run

Enter an ASCII value: 12
Exception- Non-printable character

In this example, an ASCII value entered by the user is checked for the exception

That is, if the user enters an ASCII value between 31 and 255, the corresponding

character is displayed. Otherwise, an exception is thrown and the catch block is executed

After executing the catch block, the statements after the catch block are executed in normal flow.

In addition to main the try block can also be placed in any other user-defined function. 

In this case, the try block is localised to the function. To understand this concept,

Consider this example-


Example : A program to demonstrate the concept of defining a try block inside a user-defined function

#include<iostream>
using namespace std:

void sum(int n)
{
static int sum= 0;

try                         // local try block
{

if (n>0)
sum+=n;
else throw(n);

}
catch (int i)
               {

cout<<”exception- negative number\n”;
cout<<"\n Sum of positive numbers is: “<<sum;

               }
int main()
{
int a;

cout<<"Enter the numbers”;

do
               {

cin>>a;
sum (a);    //calling the function containing the try block

} while(a>0)

return 0;
}                             //end of program

The output of the program is

Enter the numbers
4 5 10 15 2
Exception- negative number
Sum of positive numbers is: 34

In this example, the try block is defined inside the function sun() and hence, it is
local to the function.

If you want the source code of this example then ask it out in the comments section I will post it there :)…

Comments

Social Walls

Follow Me on

Share this Post

Recommended for You

Popular Posts

This is how a police chase the fugitive!

What are Crystal Giants and Why are they Deadly?

How Do We Fire Torpedoes?

How a Single-Stream Recycling Plant can save the World?

How The New Generation Road Is Made Up Of?