Rethrow an Exception

Re-throw an exception :- A handler may decide to re-throw the exception caught without processing it. In such statements we may simply invoke throw without any argument as shown below-

Syntax:-
throw;

This causes the current exception to be thrown to the next  enclosing try/catch sequence and is caught by a catch statement listed after that enclosing try block.


Cpp program for rethrow mechanism in exception handling
#include<iostream.h>
#include<conio.h>
void divide(double x,double y){
try{if (y==0.0) throw (y);
else cout<<"Division= "<<x/y<<endl;
}
catch(double y){
cout<<"Caught y and rethrow..."<<endl;
throw;
}}
void main(){
cout<<"Testing rethrow.."<<endl;
try{
divide(10.5,2.4);
divide(20.0,0.0);
}
catch(double y){
cout<<"Exception Caught y inside main function="<<endl;
}
getch();
}


No comments:

Post a Comment