tags:

views:

112

answers:

4

If I want to write useful info to a file whenever i caught a catch-all exception, how to do it?

try
{
   //call dll from other company
}
catch(...)
{
   //how to write info to file here???????
}
+4  A: 

There's no way to know anything about the specific exception in a catch-all handler. It's best if you can catch on a base class exception, such as std::exception, if at all possible.

Fred Larson
+12  A: 

You can't get any information out of the ... catch block. That is why code usually handles exceptions like this:

try
{
    // do stuff that may throw or fail
}
catch(const std::runtime_error& re)
{
    // speciffic handling for runtime_error
    std::cerr << "Runtime error: " << re.what() << std::endl;
}
catch(const std::exception& ex)
{
    // speciffic handling for all exceptions extending std::exception, except
    // std::runtime_error which is handled explicitly
    std::cerr << "Error occurred: " << ex.what() << std::endl;
}
catch(...)
{
    // catch any other errors (that we have no information about)
    std::cerr << "Unknown failure occured. Possible memory corruption" << std::endl;
}
utnapistim
+1 for showing a code structure.
Vite Falcon
A: 

I think he wants to make it log that an error occurred, but doesn't specifically need the exact error (he would write his own error text in that case).

The link DumbCoder posted above has at tutorial that will help you get what you're trying to achieve.

Marcin
+1  A: 

You can't get any details. The whole point of catch(...) is to have such "I don't know what can happen, so catch whatever is thrown". You usually place catch(...) after catch'es for known exception types.

sharptooth