How to catch exceptions conditionally?

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)?

+6
source share
2 answers

As suggested by CompuChip , Visual Studio can break execution when throwing an exception, and not just catching an uncaught one!

To enable this (in Visual Studio 2012):

  • From the menu, go to Debug β†’ Exceptions
  • In the window that opens, check the "Abandoned" box for all C ++ exceptions (ticking only std::exception not enough - I don’t know why)
  • Run the program
0
source

So, how can I make exception handling conditional (depending on the command line argument, for example)?

Writing the code for him: o]

Consider this original code:

 int main() { try { run_the_application(); // this part different than your example } catch (std::exception& e) { std::cout << "Fatal error: " << e.what() << (some more data) << std::endl; return 1; } } 

New code:

 template<typename F> int fast_run(F functor) { functor(); return EXIT_SUCCESS; } template<typename F> int safe_run(F functor) { try { functor(); } catch (std::exception& e) { std::cout << "Fatal error: " << e.what() << (some more data) << std::endl; return EXIT_FAILURE; } return EXIT_SUCCESS; } template<typename F> int run(const std::vector<std::string>& args, F functor) { using namespace std; if(end(args) != find(begin(args), end(args), "/d")) return fast_run(functor); else return safe_run(functor); } int main(int argc, char** argv) { const std::vector<std::string> args{ argv, argv + argc }; return run(args, run_the_application); } 
0
source

Source: https://habr.com/ru/post/983596/


All Articles