函数抛出异常时,throw后面的代码段不会被执行,但是throw所在函数内的局部对象的析构函数会被调用
void Exception()
{
//Inherit 是一个类,创建时会调用构造函数
Inherit test;
string str = "test";
cout<<" product exception "<<endl;
//抛出异常后会离开此函数区域,相当于return,但是不会返回任何地方,直到有catch捕捉到此错误或退出main
//有catch捕捉才会调用析构函数,如果直到程序退出也没有catch捕捉到当前异常,不会调用析构函数
throw str;
//该语句不会被打印
cout<<"throw end"<<endl;
}
int main()
{
try{
Exception();
}
catch(string str_err){
cout<<"catch exception "<<str_err<<endl;
}
return0;
}