栈解旋概念:当throw抛出异常后,创建的class类对象就会被析构。
案例:
class Person {
public:Person() {cout << "对象构建&#xff01;" << endl;}~Person() {cout << "对象析构!" << endl;}
};
int divide(int x, int y) {Person p1, p2;if (y &#61;&#61; 0) {throw y;}return x / y;
}
void test01() {try {divide(10, 0);}catch (int e) {cout << "异常捕获&#xff01;" << endl;}
}
int main() {test01();
}
结果&#xff1a;