1 #include<iostream> 2 3 #include<cstdlib> 4 using namespace std; 5 ////非安全退出,结束进程, 6 //C++ 必须释放对象,最后结束,安全退出 7 8 class pstr 9 { 10 int *p; 11 public: 12 pstr() 13 { 14 cout << "构造" << endl; 15 p = new int[1024 * 1024]; 16 } 17 ~pstr() 18 { 19 cout << "析构" << endl; 20 21 delete[] p; 22 23 } 24 }; 25 void run3(int i) 26 { 27 if (i == 0) 28 { 29 //throw 1; 30 exit(0); //非安全退出,结束进程 31 //安全退出,释放对象内存,再结束进程 32 } 33 else 34 { 35 cout << "run3" << endl; 36 } 37 38 } 39 void run2(int i) 40 { 41 pstr p2; 42 run3(0); 43 } 44 45 void run1(int i) 46 { 47 pstr p1; 48 run2(i); 49 } 50 51 void main() 52 { 53 try 54 { 55 run1(0); 56 } 57 catch (int i) 58 { 59 cout << i << "error"; 60 61 } 62 63 std::cin.get(); 64 exit(0); 65 }