It is possible for try blocks to be nested. With nested try blocks, it is sometimes necessary for an inner exception handler to pass an exception to an outer exception handler. Sometimes, both an inner and an outer catch block must perform operations when a particular exception is thrown. These situations require that the inner catch block rethrow the exception so the outer catch block has a chance to catch it.
A catch block can rethrow an exception with the throw;
statement.
#include <iostream.h>
void MyHandler()
{
try
{
throw “hello”;
}
catch (const char*)
{
cout <<”Caught
exception inside MyHandler\n”;
throw; //rethrow char* out of function
}
}
int main()
{
cout<< “Main
start”;
try
{
MyHandler();
}
catch(const char*)
{
cout
<<”Caught exception inside Main\n”;
}
cout <<
“Main end”;
return 0;
}
Output :
Main start
Caught exception inside MyHandler
Caught exception inside Main
Main end
No comments:
Post a Comment