I am wondering if there is a way to automatically terminate the program entirely when an exception is thrown, rather than having to choose between the "abort", "retry", and "ignore" buttons on the Visual C library window that appears?
(example image)
Is there any solution for this? (aside from the obvious -- fixing my code!)
CodePudding user response:
Yes, you can suppress the message box by passing _OUT_TO_STDERR
to _set_error_mode()
.
In case you still get a message box telling you that abort()
was called and you want to disable it, too, you additionally need to call _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG)
.
Full example:
#include <cassert>
#include <cstdlib>
#include <crtdbg.h>
int main()
{
_set_error_mode(_OUT_TO_STDERR);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
assert(false);
}
CodePudding user response:
SetBreakOnAssert(false)
and a whole lot of other things you can (and must) do to prevent this. The problem is that there are many different kind of error mechanisms and error windows.
The one you posted is an ASSERT, which you could disable with SetBreakOnAssert. But you'll still have to deal with the error situation, i.e. terminate the app. There are other cases where the default Windows dialog pops up.
std::set_terminate
lets you set a function that get's called when any part of your program wants to terminate the application (for example due to a double exception).
_set_se_translator
lets you specify a function that can translate a Win32 exception such as "Access Violation" to a C exception.
_set_invalid_parameter_handler
lets you specify a function that is called whenever your program calls a std function with invalid parameters. (I think out-of-bounds checking falls into that category).