zoukankan      html  css  js  c++  java
  • C++之异常处理

     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
     
    /*
        名称:C++ 异常处理
        作者:Michael Joessy
        日期:2017-06-06
        知识:
        异常:程序在运行期出现的错误
        异常处理:对有可能发生异常的地方做出预见性的安排
        你如果不处理,就会传给操作系统暴力处理,如崩溃。
        关键字:try...catch...  throw
                尝试  捕获      抛出异常
        基本思想:主逻辑与异常逻辑处理分离
        try与catch是一对多的关系
    */


    #include <iostream>
    #include <string>
    using namespace std;

    void fun1()
    {
        
    throw 1;
    }

    char getChar(const string& strA, const int nIndex)
    {
        
    if (nIndex > strA.size())
        {
            
    throw string("invalid index!");
        }
        
    return strA[nIndex];
    }

    class Excepction
    {
    public:
        Excepction(){}
        
    virtual ~Excepction(){}
        
    virtual void printException()
        {
            cout << 
    "Exception->printException" << endl;
        }
    };

    class indexException : public Excepction
    {
    public:
        
    virtual void printException()
        {
            cout << 
    "Exception -- 下标越界!" << endl;
        }
    };

    void test()
    {
        
    throw indexException();
    };

    int main(void)
    {
        
    /************************************************************************/
        
    /* 常见异常
            数组下标越界
            除数为0
            内存不足

        /* 异常处理与多态的关系
            Exception接口类,子类如下:
            HardwareErr
            SizeErr
            MemoryErr
            NetWorkErr
            抛出子类异常时,可以用父类来捕获子类的异常!
        /************************************************************************/

        
    try
        {
    #if 0
            fun1();
    #endif

    #if 0
            string str(
    "Michael Joessy");
            
    char ch = getChar(str, 100);
            cout << ch << endl;
    #endif

    #if 0   
            test();
    #endif
            
        }
        
    catch (int)
        {
            cout << 
    "Exception:int" << endl;
        }
        
    catch(double)
        {
            cout << 
    "Exception:double" << endl;
        }
        
    catch(string& str)
        {
            cout << str << endl;
        }
        
    catch(Excepction& e)      //基类捕获所有子类的异常信息 Go
        {
            e.printException();
        }
        
    catch(indexException& e)
        {
            e.printException();
        }
        
    catch(...)
        {

        }

        cin.get();
        
    return 0;
    }
  • 相关阅读:
    最短路径
    图解最小生成树
    图解最小生成树





    线索二叉树
    二叉树
  • 原文地址:https://www.cnblogs.com/MakeView660/p/6950324.html
Copyright © 2011-2022 走看看