(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...

The dup system call copies

A. an array entry B. the contents of a file

Computer Science & Information Technology

Soft skills are specific, measurable skills such as configuring and troubleshooting systems

Indicate whether the statement is true or false

Computer Science & Information Technology

In a fire suppression system, what term is used to describe what is typically a foaming chemical, gas, or water that is sprayed everywhere to put out a fire?

a. fire extinction agent b. fire suppression agent c. extinguishing medium d. eliminating factor

Computer Science & Information Technology

When phrasing interview questions, a systems analyst should avoid _____ that suggest or favor a particular reply.

A. range-of-response questions B. open-ended questions C. closed-ended questions D. leading questions

Computer Science & Information Technology