Rethrowing an Exception in C++

Estudies4you
Rethrowing an Exception
 RETHROWING AN EXCEPTION 
19. Explain about rethrowing an exception. 
Answer: 
Rethrowing Exception 
In the program execution, when an exception received by catch block is passed to another exception handler then such situation is referred to as rethrowing of exception. 
This is done with the help of following statement, 
throw; 
The above statement does not contain any arguments. This statement throws the exception to next try catch block. 

Program 

#include<iostrearn> 

using namespace std; 

void subtract(int p, int q) //A function called subtract with two arguments is defined 

cout<<"The subtract() function contains\n"; 

try //try block1

if(p==0) //This condition checks whether p is equal to zero or not. 

//if yes throw an exception else perform subtraction 

throw p; //This statement throws an exception 

else 

cout<<"The result of subtraction is:" <<p-q<<"\n"; 

}

catch(int) //This statement catches the exception throw it again to the next try block 

{

cout<<"NULL value is caught\n"; 

throw;. 

}

cout<<."End of subtract()\n\n"; 

}

int main( ) 

Cout<<"\n The main() function contains\n"; 

try 

{

subtract(5, 3); //passing integer values to subtract 

subtract(0, 2); 

}

catch(int) //This statement catches the rethrown exception 

{

cout<<"NULL value caught inside main()\n"; 

}

cout<<End of main() function \n"; 

return 0: 

}

Output 
examples of Rethrowing an Exception in c++,c++ notes unitwise,oops using c++ notes jntuh unitwise,use of Rethrowing an Exception in c++,programs for Rethrowing an Exception in c++

To Top