'exception thrown in std::thread can't trigger function set by SetUnhandledExceptionFilter
I use VS2019 on Win10
Code:
LONG __stdcall DummyUnhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
{
printf("DummyUnhandledExceptionFilter called\n");
return EXCEPTION_CONTINUE_SEARCH;
}
int wmain(const int argc, const wchar_t* argv[])
{
SetUnhandledExceptionFilter(DummyUnhandledExceptionFilter);
std::thread crash(
[]()
{
Sleep(1 * 1000);
#if 0
RaiseException(0, 0, 0, 0); //可以触发
#elif 1
throw std::exception(); //不能触发
#else
int* p = 0;
p[0] = 1; //可以触发
#endif
}
);
crash.join();
}
out of main thread, those three branches code with #if have different behavers,throw std::exception can't trigger DummyUnhandledExceptionFilter, but others does.
In Windows via C++ 5th, Chapter25 C++ Exceptions vs. Structured Exceptions, Jeffrey said:
However, you should know that Microsoft's Visual C++ compiler has implemented C++ exception handling using the operating system's structured exception handling....In fact, when you write a C++ throw statement, the compiler generates a call to the Windows RaiseException function. ...
if him right, throw std::exception have nothing special, but they do have some different. I set breakpoint at asm view, throw called RaiseException, but why it can't trigger DummyUnhandledExceptionFilter?
And std::set_terminate give no help at all.
I'm so confused, please help me! thx all
Solution 1:[1]
https://developercommunity.visualstudio.com/t/setunhandledexceptionfilter-callback-not-invoked-f/235602 This is a bug in msvc, which has been reported to Microsoft. The specific reason is stated in the post
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user17239868 |
