(Rethrowing Exceptions) Write a program that illustrates rethrowing an exception.

What will be an ideal response?

```
#include
#include
using namespace std;

// class TestException definition
class TestException : public runtime_error
{
public:
// constructor specifies error message
TestException::TestException( const string& message )
: runtime_error( message ) {}
}; // end class TestException
```
```
#include
#include "TestException.h"
using namespace std;

// function f throws a TestException
void f()
{
throw TestException( "Test exception thrown" );
} // end function f

// function g calls function f
// and rethrows any exception thrown from f
void g()
{
try
{
f();
} // end try
catch ( ... ) // catch any exception and rethrow it
{
cerr << "Exception caught in function g(). Rethrowing...\n";
throw;
} // end catch
} // end function g

int main()
{
try
{
g(); // start function call chain
} // end try
catch ( ... ) // catch any exception thrown from g
{
cerr << "Exception caught in function main()\n";
} // end catch

return 0;
} // end main
```
Exception caught in function g(). Rethrowing...
Exception caught in function main()

Computer Science & Information Technology

You might also like to view...

Combinations of formatting effects that can be applies to SmartArt graphics are called:

A) SmartArt Styles. B) SmartArt Templates. C) SmartArt Themes. D) WordArt Styles.

Computer Science & Information Technology

Which was the first event to signal the potential of computer crime?

a. the crash of AT&T b. the destruction caused by the Morris worm c. the hacking of the MILNET system d. the victimization of Michigan Bell

Computer Science & Information Technology