Re: Getting exception object
Maxim wrote:
Hi, all!
How do I get exception type and exception object in the following code:
try
{
}
catch(...)
{
I_need_exception_object_here;
}
Re-throw the exception and catch it with a more specific handler. If
you're using catch(...) in order to provide some common behavior for all
possible exceptions, you do the whole thing within the catch(...) block.
You have to know ahead of time what static exception types might be
thrown; i.e., as far as I know, there is no portable way to apply RTTI
to the exception. One of the experts in this group might be able to
give a VC-specific solution.
#include <iostream>
#include <string>
int main() {
try {
throw 42;
} catch(...) {
std::string s;
try {
throw;
} catch(int i) {
s = "an int";
} catch(...) {
s = "something else";
}
std::clog << "caught " << s << '\n'; // "an int"
}
}