#include <iostream>
using namespace std;
class SomeException {
public:
~SomeException() { cout<<"Destruct"<<endl; }
private:
int i;
};
void someOtherFunction() {
if(1) throw SomeException();
}
int main() {
int * s=new int[3]; //动态内存分配
if(0) {
delete[] s; //释放动态内存
cout<<"delete[] s"<<endl;
throw SomeException(); //当前函数向外抛出异常
}
try {
someOtherFunction(); //执行一个可能抛出异常的函数
}
catch(...) {
delete[] s; //继续抛出异常, 释放动态内存
cout<<"delete[] s"<<endl;
throw; //被截获的异常, 当前函数无法处理,需要抛给主调函数
}
delete[] s; //函数正常结束前, 释放动态内存
cout<<"delete[] s"<<endl;
cout<<"Hello World!"<<endl;
return 0;
}
using namespace std;
class SomeException {
public:
~SomeException() { cout<<"Destruct"<<endl; }
private:
int i;
};
void someOtherFunction() {
if(1) throw SomeException();
}
int main() {
int * s=new int[3]; //动态内存分配
if(0) {
delete[] s; //释放动态内存
cout<<"delete[] s"<<endl;
throw SomeException(); //当前函数向外抛出异常
}
try {
someOtherFunction(); //执行一个可能抛出异常的函数
}
catch(...) {
delete[] s; //继续抛出异常, 释放动态内存
cout<<"delete[] s"<<endl;
throw; //被截获的异常, 当前函数无法处理,需要抛给主调函数
}
delete[] s; //函数正常结束前, 释放动态内存
cout<<"delete[] s"<<endl;
cout<<"Hello World!"<<endl;
return 0;
}