在 C++11 及更高版本中,exception_ptr 可以使用 检索当前异常current_exception()。是否可以在运行时确定所指向的异常的类型?
更准确地说,如何获取type_infoan 指向的异常的 a引用exception_ptr?我知道可以catch基于类型,但是程序员需要catch为所有可能的异常类型编写块,这不是这个问题的解决方案。
try {
userProvidedRuntimePlugin.executeAction(); // May throw anything
} catch (...) {
auto e = std::current_exception();
std::type_info & info = /* ??? */;
std::cerr << "Exception of type " << info.name() << " thrown." << std::endl;
}
Run Code Online (Sandbox Code Playgroud) 有没有办法在这里获得至少一些信息?
...
catch(...)
{
std::cerr << "Unhandled exception" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我把它作为我所有代码的最后手段.让它崩溃会更好吗,因为那时我至少可以得到崩溃报告?