dev*_*bmw 6 c++ exception visual-studio-2013
为什么unhandled exceptionVS 2013 没有例外,或者执行以下代码时引发的任何中止信号?
#include <thread>
void f1()
{
throw(1);
}
int main(int argc, char* argv[])
{
std::thread(f1);
}
Run Code Online (Sandbox Code Playgroud)
C++标准规定在以下情况下应调用std :: terminate:
when the exception handling mechanism cannot find a handler for a thrown exception (15.5.1)
in such cases, std::terminate() is called (15.5.2)
问题在于,在此代码中,main() 可能会在生成的线程 (f1) 之前结束。
试试这个:
#include <thread>
void f1()
{
throw(1);
}
int main(int argc, char* argv[])
{
std::thread t(f1);
t.join(); // wait for the thread to terminate
}
Run Code Online (Sandbox Code Playgroud)
此调用在 Coliru (gcc) 上终止()。
不幸的是,当遇到这种情况时,Visual Studio 2013将直接调用abort()而不是terminate()(至少在我的测试中),因此即使添加处理程序(使用std::set_handler())显然也不起作用。 我向 VS 团队报告了此事。
尽管如此,此代码仍会触发错误,而您的初始代码则不能保证。