It is possible that a single catch block may not handle an exception completely. In such a situation, the catch block on receiving an exception must pass it to another catch block by rethrowing an exception. The rethrowing expression takes the following from,
throw ;
The rethrow expression can only appear in the catch block from where it is implicitly propagated to outer try/catch sequence and is caught by that catch block which has the same type parameter. The following program illustrate how an exception is rethrown and caught.
- #include<iostream.h>
- #include<conio.h>
- void funchandler()
- {
- try
- { throw 10; }
- catch(int i)
- {
- cout<<"caught exception inside function";
- throw;
- }
- }
- int main()
- {
- cout<<"start of mail()\n";
- try
- {
- funchandler();
- }
- catch(int i)
- {
- cout<<"Rethrown exception caught is main()\n";
- }
- cout<<"End of main()";
- return 0;
- }
Start of main()
caught exception inside function
Rethrown exception caught in main()
End of main()
caught exception inside function
Rethrown exception caught in main()
End of main()
Tags : Exception handling in C++, C++ tutorials, Exception handling Tutorials
Post a Comment