I've encountered weird behavior of debugger on Windows (MSVC17), which I managed to replicate with code as simple as
#include <iostream>
int main()
{
throw std::exception();
std::cout << "Hello World!" << std::endl;
return 0;
}
When I execute the code without the debugger attached, it behaves as expected - unhandled exception terminates program.
However, when I execute it with the debugger attached, something weird happens:
This correctly reports Unhandled exception at 0x00007FFCCBB8FE3C in ConsoleApplication1.exe: Microsoft C++ exception: std::exception at memory location 0x000000753FAFFB08.
But when I press Continue, the code continues as if the exception wasn't there:
I've also tried this code on Linux with GDB, which terminated application as expected.
After some research I've found this post, which says that it is correct behavior of MSVC debugger, but then I would expect this code to behave the same:
#include <iostream>
int main()
{
try
{
throw std::exception();
}
catch (...)
{
throw;
}
std::cout << "Hello World!" << std::endl;
return 0;
}
This code correctly catches the first exception and rethrows it, but when rethrowing, I would expect debugger to ignore the exception as for the first time. It correctly reports Unhandled exception at 0x00007FFCCBB8FE3C in ConsoleApplication1.exe: Microsoft C++ exception: std::exception at memory location 0x00000056D930F9F8.
, but when continued in debugger, I get access violation (Exception thrown at 0x0000000000000000 in ConsoleApplication1.exe: 0xC0000005: Access violation executing location 0x0000000000000000.
) and the debugger gets stuck there.
Why is debugger skipping unhandled exceptions and don't terminate the program? If it is correct behavior, why it doesn't work in second case? Am I able to turn this "feature" off?
Thank you for your help!
// Edit: Adding compiler flags
/JMC /permissive- /ifcOutput "ConsoleA.5d991025\x64\Debug\" /GS /W3 /Zc:wchar_t /ZI /Gm- /Od /sdl /Fd"ConsoleA.5d991025\x64\Debug\vc143.pdb" /Zc:inline /fp:precise /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MDd /FC /Fa"ConsoleA.5d991025\x64\Debug\" /EHsc /nologo /Fo"ConsoleA.5d991025\x64\Debug\" /Fp"ConsoleA.5d991025\x64\Debug\ConsoleApplication1.pch" /diagnostics:column