• c++ 异常处理简单示例


    前言

    c++ 异常处理简单示例

    C++ 异常处理的流程,具体为:
    抛出(Throw)–> 检测(Try) --> 捕获(Catch)

    异常必须显式地抛出,才能被检测和捕获到;如果没有显式的抛出,即使有异常也检测不到。

    Code

    example 1

    #include
    using namespace std;
    int main(){
        cout<<"1--befroe try block..."<<endl;
        try{
            cout<<"2--Inside try block..."<<endl;
            throw 10;
            cout<<"3--After throw ...."<<endl;
        }
        catch(int i) {
    	    cout<<"4--In catch block1 ... exception..errcode  is.."<<i<<endl;
        }
        catch(char * s) {
            cout<<"5--In catch block2 ... exception..errcode is.."<<s<<endl;
        }
        cout<<"6--After Catch..." << endl;
        // system("pause");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    result

    1--befroe try block...
    2--Inside try block...
    4--In catch block1 ... exception..errcode  is..10
    6--After Catch...
    
    • 1
    • 2
    • 3
    • 4

    example 2

    #include
    using namespace std;
    void temperature(int t)
    {
        try{
            if(t==100) throw "It's at the boiling point.";
            else if(t==0) throw "It reached the freezing point";
            else cout<<"the temperature is OK..."<<endl;
        }
        catch(int x){cout<<"temperature="<<x<<endl;}
        catch(char const*s){cout<<s<<endl;}
    }
    int main(){
        temperature(0);						//L1
        temperature(10);						//L2
        temperature(100);						//L3
        // system("pause");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    result

    It reached the freezing point
    the temperature is OK...
    It's at the boiling point.
    
    • 1
    • 2
    • 3

    example 3

    #include
    using namespace std;
    void temperature(int t)
    {
        
        if(t==100) throw "It's at the boiling point.";
        else if(t==0) throw "It reached the freezing point";
        else cout<<"the temperature is OK..."<<endl;
        
    }
    int main(){
        try{
            temperature(0);						//L1
            temperature(10);						//L2
            temperature(100);						//L3
        }
        catch(char const*s){cout<<s<<endl;}
        // system("pause");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    result

    It reached the freezing point
    
    • 1

    example 4

    #include
    using namespace std;
    
    void handler(int n)throw(int,char,double){
        if(n==1) throw n;
        if(n==2) throw 'x';
        if(n==3) throw 1.1;
    }
    int main(){
        cout<<"Before handler..."<<endl;
        try{
            handler(1);
        }
        catch(int i){ cout<<"catch an integer..."<<endl;}
        catch(char c){cout<<"catch an char..."<<endl;}
        catch(double d){cout<<"catch an double..."<<endl;}
        // system("pause");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    Before handler...
    catch an integer...
    
    • 1
    • 2

    example 5

    #include
    using namespace std;
    void Errhandler(int n)throw(){
        try{
            if(n==1) throw n;
            if(n==2) throw "dx";
            if(n==3) throw 1.1;
        }
        catch(...){cout<<"catch an exception..."<<endl;}
    }
    int main(){
        Errhandler(1);
        Errhandler(2);
        Errhandler(3);
        // system("pause");
        return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    result

    catch an exception...
    catch an exception...
    catch an exception...
    
    • 1
    • 2
    • 3

    example 6

    //Eg10-9.cpp
    #include
    using namespace std;
    class A{
        int a;
    public:
        A(int i=0):a(i){}
        ~A(){cout<<"in A destructor..."<<endl;}
    };
    class B{
        A obj[3];
        double *pb[10];
        public:
            B(int k){
                cout<<"int B constructor..."<<endl;
                for (int i=0;i<10;i++){
                    pb[i]=new double[20000000];
                    if(pb[i]==0)
                        throw i;
                    else 
                        cout<<"Allocated 20000000 doubles in pb["<<i<<"]"<<endl;
                }
            }
    };
    int main(){
        try{
            B b(2);
        }
        catch(int e){
            cout<<"catch an exception when allocated pb["<<e<<"]"<<endl;   
        }
        system("pause");
    }
    
    • 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

    result

    int B constructor...
    Allocated 20000000 doubles in pb[0]
    Allocated 20000000 doubles in pb[1]
    Allocated 20000000 doubles in pb[2]
    Allocated 20000000 doubles in pb[3]
    Allocated 20000000 doubles in pb[4]
    Allocated 20000000 doubles in pb[5]
    Allocated 20000000 doubles in pb[6]
    Allocated 20000000 doubles in pb[7]
    Allocated 20000000 doubles in pb[8]
    Allocated 20000000 doubles in pb[9]
    in A destructor...
    in A destructor...
    in A destructor...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    example 7

    栈示例

    //Eg10-11.cpp
    #include 
    using namespace std;
    const int MAX=3;
    class Full{
        int a;
        public:
            Full(int i):a(i){}
            int getValue(){return a;}
    };
    class Empty{};
    class Stack{
        private:
            int s[MAX];
            int top;
        public:
            Stack(){top=-1;}
            void push(int a){
                if(top>=MAX-1)  
                    throw Full(a);			
                s[++top]=a;
            }
            int pop(){
                if(top<0)
                    throw Empty();
                return s[top--];
            }
    };
    int main(){
        Stack s;
        try{
            s.push(10);  
            s.push(20);
            s.push(30);  
            s.push(40);
        }
        catch(Full e){	
            cout<<"Exception: Stack Full..."<<endl;
            cout<<"The value not push in stack:"<<e.getValue()<<endl; 
        }
        // system("pause");
    }
    
    • 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

    result

    Exception: Stack Full...
    The value not push in stack:40
    
    • 1
    • 2
  • 相关阅读:
    “看片”神器没了,又将有谁突出重围?
    【SpringMVC笔记13】SpringMVC集成Freemarker模板引擎
    webrtc一对一视频通话功能实现
    Task Flow使用指南之五:捕获异常
    不同类型的RFID标签及其应用场景浅析
    【车载开发系列】UDS诊断---读取数据($0x22)
    ActivityThread应用进程
    Linux--进程间通信
    ubuntu开启远程访问
    淘宝天猫API:buyer_cart_add-添加到购物车
  • 原文地址:https://blog.csdn.net/weixin_45063703/article/details/127627058