<分区>
Possible Duplicate:
Is the destructor called if the constructor throws an exception?
我有一个问题要问你。当构造函数抛出异常时,是否执行析构函数?示例代码:
#include <cstdio>
struct Test
{
Test (void)
{
throw 100;
}
~Test (void)
{
printf ("~Test\n");
}
};
int main (void)
{
try
{
Test test;
}
catch (int value)
{
}
}
运行此代码时,“~Test”不会显示。好的,我可能理解这种行为。假设 Test 具有动态分配的成员,这些成员在构造函数内部分配并在析构函数中删除。当它们在构造函数中分配后抛出异常时,它们会发生什么?