Hello,
I'm trying to catch unhandled exceptions from a thread created with C++11
std::thread using SetUnhandledExceptionFilter.
But it does not work.
When I create the thread with the CreateThread function and throw the same exception inside. it works fine.
Does anybody know why it does not work with std::thread?
A minimal example of the code I'm using is attached to this message.
Best regards
Markus
#include <Windows.h>
#include <DbgHelp.h>
#include <iostream>
#include <thread>
LONG exceptionFunc(LPEXCEPTION_POINTERS p)
{
std::cout << "EXCEPTION_EXECUTE_HANDLER" << std::endl;
Sleep(10000);
return EXCEPTION_EXECUTE_HANDLER;
}
DWORD WINAPI MyThreadFunction( LPVOID lpParam )
{
Sleep(5000);
throw 0;
}
#define CPPRUNTIME 1
int main(int argc, char* argv[])
{
SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)&exceptionFunc);
#ifndef CPPRUNTIME
DWORD dwThreadIdArray;
CreateThread(
NULL, // default security attributes
0, // use default stack size
MyThreadFunction, // thread function name
NULL, // argument to thread function
0, // use default creation flags
&dwThreadIdArray); // returns the thread identifier
#else
auto handle = std::thread([&]()
{
Sleep(5000);
throw 0;
});
handle.join();
#endif
while(1)
{
Sleep(1000);
}
return 0;
}