Originally Posted by
MarkZWEERS
Honestly? I thought that the propagation of thrown exceptions was bad coding practice.
Exactly the opposite. If a functionA() calls function B(), functionB() throws an exception, and functionA() cannot rationally recover from exception (which means it cannot recover from the conditions that caused the exception), then the exception should be allowed to propagate.
To answer the original it is possible to write a function that enables exception handling code, thus;
Code:
try
{
functionThatThrows();
}
catch(...)
{
Handler();
}
void Handler() // called ONLY if an exception is active
{
try
{
throw; // rethrows the active exception. abort()'s if no active exception
}
catch(Exception1 &)
{
// handle Exception1
}
catch (Exception2 &)
{
// handle Exception2
}
// etc
}
This doesn't completely eliminate the need for a try/catch block in every function that needs to do the same exception handling, but it does allow writing a series of handlers only once.