cpp re-throwing An exception

C++ Re-throwing An exception

A handler may decide to re-throw the exception caught without processing it. In such situations, we may simply invoke throw without any arguments as shown below

throw;

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

take the example how an exception is re-thrown and caught

RE-THROWING AN EXCEPTION
#include <iostream>
using namespace std;
void divide(double x, double y)
{
cout << "Inside function \n";
try
{
if (y==0.0)
throw y; // throwing double
else
cout << "Division=" << x/y << "\n";
}
catch (double)
// catch a double
{
cout << "caught double inside function \n";
throw ; // re-throwing double
}
cout << "End of function \n \n";
}
int main()
{
cout << "Inside main \n";
try
{
divide(10.5,2.0);
divide(20.0,0.0);
}
catch (double)
{
cout << "Caught double inside main \n";
}
cout << "End of main \n";
return 0;
}

output:
Inside main
Inside function
Division=5.25
End of function

Inside function
caught double inside function
caught double inside main
End of main

When an exception is re-thrown, it will be caught by the same catch statement or nay other catch in that group Rather it will be caught by an appropriate catch in the outer try catch sequence only

A catch handler itself may detect and throw an exception. Here again the exception thrown will not be caught by any catch statements in that group. It will be passsed on to the next outer try.catch sequence for processing

  • 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