VC ++ std :: current_exception在未捕获的异常中返回null

时间:2018-07-10 10:20:10

标签: c++ windows exception

似乎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和Clang ++中效果很好(也可以打印异常的内容),但是在VC ++中,它只能打印:

term_func was called by terminate.(1)
term_func was called by terminate.(2)

现在,对此有任何解决方法吗?还是最初的问题?

0 个答案:

没有答案