My large application has this structure:
int main() { try { ... } catch (std::exception& e) { std::cout << "Fatal error: " << e.what() << (some more data) << std::endl; return 1; } }
Inside the call stack, various objects check their internal state by throwing std::runtime_exception if they find something bad. A comprehensive exception handler catches it, prints some moderately useful information, and terminates the program.
However, when I debug under MS Visual Studio, I could benefit from the lack of an exception handler: Visual Studio has its own, very useful, handler that stops my application at the place where the exception is thrown, so I can check what went not this way.
How can I bind my exceptions conditionally?
I tried the following:
try { ... } catch (std::exception& e) { if (IsDebuggerPresent()) throw; else std::cout << "Fatal error: " << e.what() << (some more data) << std::endl; }
This gave a strange result: Visual Studio caught the exception that was being repaired and showed me the stack trace at the point where the exception was thrown. However, all the objects in my application were apparently destroyed, and I could not see, for example. local or member variables.
I could make the exception handler conditional on the compilation flag:
#ifdef NDEBUG try { #endif ... #ifdef NDEBUG } catch (std::exception& e) { std::cout << "Fatal error: " << e.what() << (some more data) << std::endl; } #endif
but this is inconvenient because I need to recompile everything if I want to debug it.
So, how can I make exception handling conditional (depending on the command line argument, for example)?