C++ catch blocks mechanism

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

catch(type arg)
{
// statement for
// managing exception
}

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

try
{
// try block
}
catch(type1 arg)
{
// catch block1
}
catch(type2 arg)
{
// catch block2
}
….
….
catch(typeN arg)
{
//catch block N
}

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
#include < iostream >
using namespace std;
void test(int x)
{
try
{
if(x==1) throw x; // int
else
if(x==0) throw 'x'; // char
else
if(x==-1) throw 1; // double
cout<<"END of try block \n";
}
catch(char c) // catch 1
{
cout << "caught a character \n";
}
catch(int m) // catch 2
{
cout << "caught an integer \n";
}
catch(double d) // catch 3
{
cout << "caught a double \n";
}
cout << "End of try catch system \n\n";
}
int 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

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise