Windows中的默认终止处理程序似乎没有打印导致它的异常的ex.what()。
作为解决方法,我想实现一个自定义终止处理程序来打印异常:
#include <iostream>
void term_func()
{
std::cout << "term_func was called by terminate.(1)" << std::endl;
std::exception_ptr eptr = std::current_exception();
try
{
if(eptr)
{
std::rethrow_exception(eptr);
}
}
catch(const std::exception& e)
{
std::cout << "Caught exception \"" << e.what() << "\"\n";
}
std::cout << "term_func was called by terminate.(2)" << std::endl;
exit(-1);
}
int main(int argc, char **argv)
{
std::set_terminate(term_func);
throw std::runtime_error("is windows broken?");
}
这在GCC和CLAN+++中也很好(打印了异常的内容),但是在VC++中它只打印:
term_func was called by terminate.(1)
term_func was called by terminate.(2)
现在,有什么解决办法吗?还是最初的问题?