boost:exception使用实例
2016-04-28 16:14
302 查看
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 }
相关文章推荐
- ln: creating hard link 问题
- boost实现串口通信(一):小试牛刀
- std::cout彩色输出
- C++读取ini文件的类
- C++记录debug信息的log类
- C++三大库boost、loki、stlport
- VC++2010下编译STLport,Boost
- 百度面试问题小结
- 不惑JAVA之JAVA基础 - 反射
- 栈的压入、弹出序列
- 看懂此文,不再困惑于javascript中的事件绑定、事件冒泡、事件捕获和事件执行顺序
- FFTW程序Demo
- unity3D——自带寻路Navmesh入门教程(二)(转)
- Windows下使用gvim编写Python程序缩进的设置
- 优秀的开源项目整理 ( 更新中 )
- The type sun.management.ManagementFactory is not visible 报错问题
- 合并表记录(map)
- 关于类中域使用基本类型还是包装类的纠结
- 怕自己遗忘的信息
- Android帧动画animation-list在ProgressBar上失效的解决办法