C++中 exception 的使用

本文详细介绍了C++标准库中的异常处理机制,包括std::exception类及其派生类的继承结构,并通过示例展示了如何使用标准库异常及自定义异常来处理错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值