boost::exception

boost::exception 异常 P151

#include <boost/exception/all.hpp>
using namespace boost;

  • 代码

        #include <string>
        #include <boost/exception/all.hpp>
    
        using namespace boost;
    
        struct my_exception :           //自定义异常类
            virtual std::exception,     //虚继承
            virtual boost::exception    //虚继承
        {};                             //空实现,不需要实现代码
    
        //异常信息的类型
        typedef boost::error_info<struct tag_err_no, int>  err_no;
        typedef boost::error_info<struct tag_err_str, std::string> err_str;
    
        int main()
        try
        {
            try
            {
                throw my_exception() << err_no( 10 );
            }
            catch (my_exception& e)
            {
                //获得异常内存存储的信息
                cout << *get_error_info<err_no>( e ) << endl;
                cout << e.what() << endl;
                e << err_str( "other info" );   //向异常追加信息
                throw;                          //再次抛出异常
            }
    
    
            system( "PAUSE" );
            return 0;
        }
        catch( my_exception& e )
        {
            cout << *get_error_info<err_str>( e ) << endl;
        }
  • boost预定义异常信息类型 P156

  • 辅助工具宏 DEFINE_ERROR_INFO

        #define DEFINE_ERROR_INFO(type, name) \
          typedef boost::error_info<struct tag_##name, type> name
    
        //声明一个变量
        DEFINE_ERROR_INFP(int, err_no); //相当于typedef boost::error_info<struct tag_err_no, int> err_no;
  • 获得更多的调试信息 P159

    diagnostic_information(). //cout<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值