c++ 异常处理简单示例

前言

c++ 异常处理简单示例

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

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

Code

example 1

#include<iostream>
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;
}

result

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

example 2

#include<iostream>
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;
}

result

It reached the freezing point
the temperature is OK...
It's at the boiling point.

example 3

#include<iostream>
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;
}

result

It reached the freezing point

example 4

#include<iostream>
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;
}
Before handler...
catch an integer...

example 5

#include<iostream>
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;
} 

result

catch an exception...
catch an exception...
catch an exception...

example 6

//Eg10-9.cpp
#include<iostream>
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");
}

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...

example 7

栈示例

//Eg10-11.cpp
#include <iostream>
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");
}

result

Exception: Stack Full...
The value not push in stack:40
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nsq_ai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值