C++ Catch blocks mechanism
As stated earlier, code for handling exception is included in catch blocks. A catch block looks like any function definition who catch the exception and is of the form
The type indicates the type of the exception that catch block handles. The argument arg is an optional parameter name.
Note that the exception handling code is placed between two braces.
The catch statement catches an expression whose type matches with the type of catch argument. When it is caught, the code inside the catch block is executed
If the parameter in the catch statement is named, then the parameter can be use in the code of Exception handling. After execution the handler, the control goes to The statement immediately following the catch block
due to mismatch, if an exception is not caught, abnormal program termination will occur. It is important to
note that the catch block is simply skipped if the catch statement does not catch an exception
Multiple catch statements
it is also possible that a program segment has more than one condition to throw an exception show in below example. In such type cases, we can associate more than one catch statement with a try(much like the conditions in a switch statement) as shown below
{
}
(type1 arg)
{
}
(type2 arg)
{
}
….
….
(typeN arg)
{
}
|
whenever an exception is thrown, The exception handlers are searched in order for an appropriate match.
The first handler that yields a match is executed. After executing the error handler, Then control jumps to the first
statement after last catch block for that try. When no match is found, the program will terminate
if previous condition not true then it is possible that arguments of several catch statements match the type of an exception.
In such type cases, the first handler that matches the exception type is executed.
Take a example where multiple catch statements are used to handled various types of exceptions
MULTIPLE CATCH STATEMENTS
< iostream >
using namespace std;
test( x)
{
{
(x==1) x;
(x==0) 'x';
(x==-1) 1;
cout<<"END of try block \n";
}
( c)
{
cout << "caught a character \n";
}
( m)
{
cout << "caught an integer \n";
}
( d)
{
cout << "caught a double \n";
}
cout << "End of try catch system \n\n";
}
main()
{
cout << "Testing multiple catches \n";
cout << "x==1 \n";
test(1);
cout << "X==0 \n";
test(0);
cout << "X==-1 \n";
test(-1);
cout << "x==2 \n";
test(2);
}
|
output:
Testing Multiple catches
x==1
caught an integer
End of try catch system
x==0
caught a character
End of try catch system
x==-1
caught a double
End of try catch system
x==2
End of try-block
End of try-catch system
|
The program when executed first, invokes the function test() with x=1 and therefore throws X an int exception. This matches the type of the parameter m in catch2 and therefore catch2 handler is executed.
Immediately after the execution, the function test() is again invoked with x=0. this time the function throws ‘x’ a character type exception and therefore the first handler is executed. Finally the handler catch3 is executed when a double type exception is thrown. Note that every time only the handler which catches the exception is executed and all other handlers are bypassed
When the try block does not throw any exceptions and it completes normal execution, control passses to the first statement after the last catch handler associated with that try block
Note Try block does not throw any exception, when the test() is invoked with x=2