引发导致调用析构函数的异常会使程序崩溃
Throwing an exception which causes a destructor to be called crashes the program
考虑这一小段代码,它实际上是更大代码库的一部分:
class A
{
public:
A()
{
std::cout << "A" << std::endl;
}
~A()
{
std::cout << "~A" << std::endl;
}
};
void f()
{
A a;
throw;
}
void g()
{
try
{
f();
}
catch(...)
{
std::cout << "Caught" << std::endl;
}
}
对于我的特定情况,输出结果是
A
~A
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
似乎不是捕获异常,而是程序刚刚终止。但是,如果我删除 A 的构造函数,异常确实会被捕获。
如果不仔细分析代码,是否有可能知道是什么导致了这种行为?
一个没有操作数的抛出表达式,就像在你的代码中一样:
- 重新引发当前处理的异常(同一对象,而不是它的副本)
- 或者,如果当前没有处理异常,则调用
std::terminate
。
我假设在处理异常时没有调用f()
(我想您直接从main
或其他东西调用它)。因此,std::terminate
被称为。
对象a
无关紧要。
相关文章:
- 为什么异常不退出程序?
- 在信号处理程序中捕获C++未处理的异常并恢复应用程序
- 来自 Android 应用程序内部的 boost 类型的 boost::wrapexcept<boost::system::system_error> 的未捕获异常
- 在C++程序中使用的迭代器中未处理的异常
- 与异常处理程序中的操作员<<不匹配
- 捕获异常后如何退出程序执行
- 捕获全部处理程序检测C++中的子类异常
- 数组 C++ 上的异常处理程序
- SDL_Image库会使程序崩溃或使其行为异常
- 从终止处理程序访问所有未捕获的异常
- 引发未经处理的异常:简单 C++ 程序中的读取访问冲突,动态增加数组长度以存储数字
- 使用 C++ dll 运行 c# 应用程序会生成无法加载异常
- 视觉 std::矢量无异常:警告 C4530:使用了C++异常处理程序,但未启用展开语义.指定 /EHsc
- 奇怪的消息 (_Base_bitset::_M_do_to_ulong) 从溢出异常处理程序中打印出来
- 从处理程序捕获时如何获取 Ada 异常消息C++?
- 核心消息传递中未处理的异常.dll在程序关闭期间
- C++ 未捕获异常,程序将终止并中止
- C++程序在第一次尝试时会给出垃圾,但如果它捕获异常并重试,则会给出适当的值
- 如何处理异步函数中的异常UWP应用程序getFileFrompathAsync(path);
- 未处理异常- c++程序在强制转换时停止