C++中 exception 的使用

std:: exception ,定义于头文件 <exception>,它提供一致的接口,以通过 throw 表达式处理错误。

标准库所生成的所有异常继承自 std::exception。

其继承结构如下:

成员函数

(构造函数)

构造异常对象
(公开成员函数)

(析构函数)

[虚]

析构该异常对象
(虚公开成员函数)

operator=

复制异常对象
(公开成员函数)

what

[虚]

返回解释性字符串
(虚公开成员函数)

 

示例:标准库exception和自定义exception的使用。

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

const string egg="I not like this number,so I decided to refuse it.";

class non_44_error: public logic_error{
public:
    explicit non_44_error(const string &s=egg):logic_error(s){}
};

int main(){
    int input;
    while(1){
    try{
        cout<<"Please type in a number between 1 and 100."<<endl;
        cin>>input;
        if(!cin.good()){
            cin.clear();
            cin.ignore();
            throw invalid_argument("The input should be a number!");
        }
        if(input>=100)
            throw length_error("The input should be less than 100!");
        if(input<0)
            throw out_of_range("The input should be Non-negative number!");
        if(input==44)
            throw non_44_error();
        cout<<"Your input is "<<input<<". there isn't error\n";
    } catch(invalid_argument e){
        cout<<"*********************************"<<endl;
        cout<<"There is an invalid argument error occured"<<endl;
        cout<<"info:"<<e.what()<<endl;
        cout<<"*********************************"<<endl;
    } catch(length_error e){
        cout<<"*********************************"<<endl;
        cout<<"There is a length error occured"<<endl;
        cout<<"info:"<<e.what()<<endl;
        cout<<"*********************************"<<endl;
    } catch(out_of_range e){
        cout<<"*********************************"<<endl;
        cout<<"There is an out of range error occured"<<endl;
        cout<<"info:"<<e.what()<<endl;
        cout<<"*********************************"<<endl;
    } catch(non_44_error e){
        cout<<"*********************************"<<endl;
        cout<<"There is an error occured"<<endl;
        cout<<"info:"<<e.what()<<endl;
        cout<<"*********************************"<<endl;
    } catch(exception e){
        cout<<"*********************************"<<endl;
        cout<<"There is an undefined error occured"<<endl;
        cout<<"info:"<<e.what()<<endl;
        cout<<"*********************************"<<endl;
    }
        cout<<endl;
    }

    return 0;
}

输出:

Please type in a number between 1 and 99.
99
Your input is 99. there isn't any error

Please type in a number between 1 and 99.
1000
*********************************
There is a length error occured
info:The input should be less than 100!
*********************************

Please type in a number between 1 and 99.
-1
*********************************
There is an out of range error occured
info:The input should be Non-negative number!
*********************************

Please type in a number between 1 and 99.
d
*********************************
There is an invalid argument error occured
info:The input should be a number!
*********************************

Please type in a number between 1 and 99.
44
*********************************
There is an error occured
info:I don't like this number,so I decide to refuse it.
*********************************

 


版权声明:本文为qq_37968132原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。