c++异常处理--创建自己的异常处理类

复习了一下c++中的异常处理!
继承exception类
    class  myException : public std::exception
    {
    public:
        explicit myException(std::string meg) : message(meg){}
        //exception类中有一个what()虚函数,重新实现它
        virtual const char* what(){return message.c_str();}
        //不要少了这一行代码
        virtual ~myException() throw(){}
    private:
        std::string message;    
    }

--------------------------------------------------------------------
1.function() throw(){} 
/**函数后面的throw()表示此函数不会发出异常,c++11已将其摒弃,而使用noexcept关键字代替 function() noexcept{}; */
2.function() throw(bad_thing){}
/**函数后面的throw(bad_thing)表示此函数会发出bad_thing类型异常*/
3.function() throw(...){}
/**函数后面的throw(...)表示此函数会发出任意类型的异常*/
4.据<<C++ primer plus>>书中表示,noexcept关键字也可以用作运算符,判断操作数是否可能引发异常,如果操作数可能引发异常,则noexcept返回false,否则返回trueint function_1(int, int);
    int function_2(int, int) noexcept;

    noexcept(function_1) --> return false;
    noexcept(function_2) --> return true;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值