#include <iostream>
#include <stdexcept>
class Test {
public:
Test() { std::cout << "Constructor called" << std::endl; }
~Test() { std::cout << "Destructor called" << std::endl; }
};
int main() {
Test obj1;
try
{
Test obj2;
throw std::runtime_error("Exception thrown");
} // Object 2 is destroyed here
catch (...)
{
throw; // std::abort
}
} // not reached (implementation defined), Object 1 destructor never called
我明白为什么不调用obj2
的析构函数,因为如果在未捕获的异常的情况下发生堆栈展开,它就是定义的实现,但我不明白为什么在通常的Linux platforms it's allowed or thinks it has to terminate with a SIGSEGV signal上.据我了解,预计会出现SIGABRT.