简而言之,您有一个未处理(未观察到)的异常,因为在您的一项任务中引发的异常不会被任务、其延续之一或主应用程序捕获,因为异常是从任务重新抛出的::第一个任务的 get 在第二个任务调用 task::get 之前展开堆栈。
更简化的代码显示std::terminate
调用它是因为任务中抛出的异常没有得到处理。取消注释result.get()
将阻止对 的调用std::terminate
,因为task::get
将重新引发异常。
#include <pplx/pplx.h>
#include <pplx/pplxtasks.h>
#include <iostream>
int main(int argc, char* argv[])
{
try
{
auto result = pplx::create_task([] ()-> int
{
throw std::exception("task failed");
});
// actually need wait here till the exception is thrown, e.g.
// result.wait(), but this will re-throw the exception making this a valid use-case
std::cout << &result << std::endl; // use it
//std::cout << result.get() << std::endl;
}
catch (std::exception const& ex)
{
std::cout << ex.what() << std::endl;
}
return 0;
}
看看中的建议pplx::details::_ExceptionHandler::~_ExceptionHolder()
//pplxwin.h
#define _REPORT_PPLTASK_UNOBSERVED_EXCEPTION() do { \
__debugbreak(); \
std::terminate(); \
} while(false)
//pplxtasks.h
pplx::details::_ExceptionHandler::~_ExceptionHolder()
{
if (_M_exceptionObserved == 0)
{
// If you are trapped here, it means an exception thrown in task chain didn't get handled.
// Please add task-based continuation to handle all exceptions coming from tasks.
// this->_M_stackTrace keeps the creation callstack of the task generates this exception.
_REPORT_PPLTASK_UNOBSERVED_EXCEPTION();
}
}
在原始代码中,第一次调用task::get
引发了该任务中引发的异常,这显然阻止了第二次调用,task::get
因此第二个任务的异常不会得到处理(仍然“未观察到”)。
将调用第二个任务的析构函数,并反过来导致在堆栈展开期间重新抛出一个异常,从而导致调用“中止”。
第二个任务的析构函数不会重新抛出它只是调用 std::terminate() 的异常(它调用 std::abort())
看。并发运行时的异常处理