c++ - VC++ std::current_exception 在未捕获的异常中返回 null
问题描述
似乎 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)
现在,有什么解决方法吗?还是最初的问题?
解决方案
推荐阅读
- sql - 如何加快使用 DISTINCT 的 SQL 查询
- machine-learning - 是否有任何机器学习分类器通常最适合 NLP 项目?
- python-3.x - 展平一个元组并为元组的每个值保留相应的值
- python - 通过迭代列表长度并使用列表值来使用 map 而不是 for 循环
- macos - Emacs 在 Mac OSX Catalina 中停止工作。怎么修?
- python - 我可以使用 Python SDK 对 Firebase Firestore 子项进行排序吗?
- python - 为什么图像(numpy 数组)在编码为 tfrecord 文件之前转换为字符串?
- javascript - Vue.js 应用程序不会从特定方法向控制台抛出错误
- android - 如何解决 App 制作中的解析错误?
- android - Android:由于配置时的 IllegalArgumentException,无法实例化 MediaCodec