#include<iostream>
using namespace std;
class Person{
public:
Person(){
cout<<"对象构建"<<endl;
}
~Person(){
cout<<"对象析构"<<endl;
}
};
int function(int x,int y){
Person p1,p2; //定义两个栈变量
if(y==0)
throw y; //当抛出异常的时候,p1,p2的内存自动被析构掉
return x/y;
}
void test01(){
try{
function(10,0); //当try开始的时候,函数内定义的所有局部变量都被析构掉
}
catch(int e){
cout<<"除数为"<<e<<"!"<<endl;
}
}
int main(){
test01();
return 0;
}