49

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???????
}
STF
  • 1,485
  • 3
  • 19
  • 36
5YrsLaterDBA
  • 33,370
  • 43
  • 136
  • 210
  • 2
    What message would you like to get out of the exception? What if the object thrown is a `int`? When you `catch(...)`, you don't know that the exception caught will *have* a message. – jalf Jun 28 '10 at 14:09
  • 1
    This question gave me a weird thought (it wouldn't work, but it would be a funny thing if it did): template catch(const T& ex) {...} I don't think it could possibly work since exceptions are more of a runtime mechanism... or could it? The stack unwinding mechanisms involved in throwing an exception and branching to the correct catch block just seem like magic to me. Perhaps the correct catch block to branch to is still determined at compile-time, which would explain one of the reasons why it's so unsafe to throw across module boundaries. – stinky472 Jun 29 '10 at 08:27

5 Answers5

79

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 occurred. Possible memory corruption" << std::endl;
}
Max
  • 15,693
  • 14
  • 81
  • 131
utnapistim
  • 26,809
  • 3
  • 46
  • 82
18

A caught exception is accessible by the function std::current_exception(), which is defined in <exception>. This was introduced in C++11.

std::exception_ptr current_exception();

However, std::exception_ptr is an implementation-defined type, so you can't get to the details anyway. typeid(current_exception()).name() tells you exception_ptr, not the contained exception. So about the only thing you can do with it is std::rethrow_exception(). (This functions seems to be there to standardize catch-pass-and-rethrow across threads.)

Perette
  • 821
  • 8
  • 17
6

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
  • 60,987
  • 18
  • 112
  • 174
3

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
  • 167,383
  • 100
  • 513
  • 979
1

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,266
  • 3
  • 15
  • 31