boost:exception使用实例

 1 /************************************************************************/
 2 /*功能描述: boost exception使用实例                                    */
 3 /*作者    : kernel_main                                                */
 4 /*创建时间: 2014.6.8                                                   */
 5 /************************************************************************/
 6 #include <iostream>
 7 #include <exception>
 8 
 9 #include <boost/exception/all.hpp>
10 
11 struct my_exception :         /* 自定义异常类 */
12     virtual std::exception,   /* 虚继承,struct默认public继承 */
13     virtual boost::exception  /* 虚继承,struct默认public继承 */
14 {
15     /* 空实现,不需要实现代码 */
16 };
17 
18 /* 异常信息的类型 */
19 typedef boost::error_info<struct tag_err_no, int> err_no;
20 typedef boost::error_info<struct tag_err_str,std::string> err_str;
21 
22 int main()
23 {
24     using namespace boost;
25     try
26     {
27         try
28         {
29             /* 抛出异常,存储错误码 */
30             throw my_exception() << err_no(10);
31         }
32         catch (my_exception& e) /* 捕获异常,使用引用形式 */
33         {
34             std::cout << *get_error_info<err_no>(e) << std::endl;
35             std::cout << e.what() << std::endl;
36             e << err_str("other info"); /* 向异常追加信息 */
37             throw; /* 再次抛出异常 */
38         }
39     }
40     catch (my_exception& e)
41     {
42         std::cout << *get_error_info<err_str>(e) << std::endl;
43         std::cout << e.what() << std::endl;
44     }
45     return 0;
46 }

 

posted on 2014-06-08 19:54 kernel_main 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/kernel0815/p/3776656.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值