C++ exception handling Try catch

C++ exception handling Try catch

C++ exception handling mechanism is basically built on three keywords mainly, namely, try, throw, and catch

The try keyword is used to preface a block of statements which may generate exceptions. This block of statement is known as try block. when an exception is detected. it’s thrown using a throw keyword in the try block. A catch block defined by the keywords catch catches the exception thrown by the statement in the try block and handled it appropriately The relationship shown in the below fig

try catch throwing exception

the catch block that catches an exception must immediately follow the try block that throws the exception. The ordiniary form of these two blocks are as follows

……
……
try { …..
throw exception; // block of statement which
…..
….. // detectes and throws an exception
}
catch(type org) // catch exception
{
……
…… // block of statement that
…… // handles the exception
……
}
…..
…..

Whenever the try block throws an exception, the program control leaves the try block and immediately jump to the catch statement of the catch block. Note that exceptions are objects used to transmit information about problem. if the type of object thrown matches the arg type in 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 the abort() function which is invoked by default. Whenever no exception is detected and thrown, the control goes to the statement immediately after the catch statement or block. that is the catch block is skipped This simple try catch mechanism in the below program

TRY BLOCK THROWING AN EXCEPTION
#include< iostream >
using namespace std
int main()
{
int a,b;
cout << "Enter values of a and b \n";
cin >> a;
cin >> b;
int x=a-b;
try
{
if(x!=0)
{
cout << "Result(a/x)=" << a/x << "\n";
}
else
{
throw(x); // throws int object
}
}
catch(int i)
{
cout << "EXCEPTION caught:=" << x << "\n";
}
cout<<"END";
}

output:
First run
Enter values of a and b
20 15
result(a/x)=4
END
second run
Enter values of a and b
10 10
Exception caught: x=0
END

program detects and catches a division by zero problem. The output of first run shows a successful execution. When no exception is thrown, the catch block is skipped and execution resumes with the first line after the catch. In the second occurs. This exception is thrown using the object X. since The exception object is an int type, The catch statement containing int type argument catches the exception and displays necessary message

Most often, Expressions are thrown by functions that are invoked from within the try blocks. The point at which the throw by functions that are invoked from within the try blocks. The point at which the throw is executed is called the throw point. once an exception is thrown to the catch block, controls cannot return to the throw point. The kind relationship is shown in below fig

function invoke by try block throwing exception

The General Format of code for this kind of relationship is shown below

type function(arg list) // function with exception
{
…….
…….
throw (object); // throws exception
…….
…….
}
try
{

…..
….. invoke function here
…..
}
catch (type arg) // catches exception
{
…….
……. Handles exception here
…….
}

Note: The try block is immediately followed by the catch block, irrespective of the location of the throw point

In the below program show how a try block invokes a function that generates an exception

INVOKING FUNCTION THAT GENERATES EXCEPTION

// Throw point outside the try block
#include <iostream>
using namespace std;
void divide(int x, int y, int z)
{
cout << "\n we are inside the function \n";
if((x-y)!=0) // it is ok
{
int r=z/(x-y);
cout << "Result= " << r <<"\n";
}

else
// There is a problem
{

throw
(x-y); // throw point
}
}
int main()
{
try
{

cout << "we are inside the try block \n";
divide(10,20,30); // invoke divide()
divide(10,10,20); // invoke divide()
}
catch(int i)
{
cout << "caught The exception \n";
}
}

The output of the above programM
we are inside the try block
we are inside the function
result=-3
we are inside the function
caught the exception