0

In testing and debugging C++ code, there are often errors that visual studio can detect. Usually it responds by creating a breaking window pop up that let's you halt execution and inspect the call stack and values of variables etc.

I am wondering if there is any possible way to have the debugger throw an exception instead of doing it's normal break.

I want to be able to do something like:

try {
    functionThatMightBreak(); // break as in, derefence null, uninitialized variable, iterator not derefenceable etc
} catch (SomeType e) {
    // a bit more stuff
}
  • 1
    Debugger does not throw exceptions... you probably mean something else. Check "debug->exceptions" and "tools->options->debug" - you may find a way to configure debugger's behavior to your liking. – Alexei Levenkov Feb 22 '14 at 05:48
  • Can you explain what behavior you're looking for from the debugger? i don't think you want the debugger to "throw an exception" because that would cause VS to stop. – John Yost Feb 22 '14 at 05:49
  • For example, if I accidentally dereference a null pointer I don't want a window to pop up asking if I want to break. It would be nicer if it threw a NullPointerException as in Java (for example). This is desirable because I want to run several tests and even if one fails I still want to be able to run the next because they're all independent and making them separate projects is unfeasible. –  Feb 22 '14 at 05:51

3 Answers3

0

If you compile your code with the /EHa compiler option, then the compiler will generate code such that Win32 exceptions (such as access violations) will be thrown like regular C++ exceptions and can be caught by catch(...) blocks.

If you do this, be careful that all of your code is compiled with the same options (and be especially careful of any third-party libraries you're using).

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
0

The exceptions your are trying to catch (such as access violations) are os-specific. Visual Studio lets you catch them with catch(...) as an extension. You could also have a look at Structured Exception Handling.

François Moisan
  • 790
  • 6
  • 12
0

Have a look at Structured Exceptional Handling aka SEH which is offered as a VC++-specific extension to the C++ language.

cf.

Community
  • 1
  • 1
nodakai
  • 7,773
  • 3
  • 30
  • 60