zoukankan      html  css  js  c++  java
  • 138.安全退出的异常,要用throw 尽量不用exit(0)

     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 }
  • 相关阅读:
    hive metastore && hiveserver2 . 基本配置
    Flink HA 搭建坑
    protobuf 编译安装
    编译Hadoop 2.7.2支持压缩 转
    centos 6挂载磁盘
    python
    python之面向对象(一)
    python
    python-文件压缩和解压
    python-configparser模块
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8688075.html
Copyright © 2011-2022 走看看