30 lines
627 B
C++
30 lines
627 B
C++
// 栈解旋(unwinding):当抛出异常时,会调用所有对象的析构函数
|
|
#include <iostream>
|
|
#include <cstring>
|
|
#include <cstdlib>
|
|
|
|
using namespace std;
|
|
|
|
class A
|
|
{
|
|
public:
|
|
A() { cout << "A()" << endl; }
|
|
~A() { cout << "~A()" << endl; }
|
|
};
|
|
|
|
int main()
|
|
{
|
|
try
|
|
{
|
|
A a1;
|
|
// 栈解旋(unwinding):当抛出异常时,会调用所有对象的析构函数
|
|
throw 0; // 抛出异常时,则会调用 a1 的析构函数
|
|
}
|
|
catch (...) // ... 表示其他异常,可以不写,写的话必须放在最后
|
|
{
|
|
cout << "异常被处理" << endl;
|
|
}
|
|
|
|
return 0;
|
|
}
|