Possible Duplicate:
Determining exception type after the exception is caught?
Following up on this question , I'd like to print out the current exception in a catch(...) block -- just for logging. One answer there says that there is no standard way of doing this, but I don't like taking no for an answer :-)
current_exception() is a function mentioned in various places on the web but apparently not well-supported. Any thoughts on this? After all, even C has errno.
Because it can be rethrown (with a simple **throw*), the exception object must be available somehow.
I am using MSVS 9.0.
Edit: The conclusion seems to be that this is not possible.
Bookmark this question. Show activity on this post. Java does not allow us to catch the exception twice, so we got compile-rime error at //1 .
Answer: When an exception is thrown in the catch block, then the program will stop the execution. In case the program has to continue, then there has to be a separate try-catch block to handle the exception raised in the catch block.
At a time only one exception occurs and at a time only one catch block is executed. All catch blocks must be ordered from most specific to most general, i.e. catch for ArithmeticException must come before catch for Exception.
If you only care about exceptions that you know about when you're writing the code then you can write a handler that can deal with all 'known' exceptions. The trick is to rethrow the exception that you caught with catch(...)
and then catch the various known exceptions...
So, something like:
try
{
...
}
catch(...)
{
if (!LogKnownException())
{
cerr << "unknown exception" << endl;
}
}
where LogKnownException()
looks something like this:
bool LogKnownException()
{
try
{
throw;
}
catch (const CMyException1 &e)
{
cerr << "caught a CMyException: " << e << endl;
return true;
}
catch (const Blah &e)
{
...
}
... etc
return false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With