zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    团队个人冲刺day08
    4.26 jQuery AJAX load() 方法
    4.23 jquery
    4.22 AJAX技术
    4.21 正则表达式
    4.20
    4.15 重写团队作业1
    4.12 重写团队作业1
    4.9 团队作业1
    4.7 团队作业1
  • 原文地址:https://www.cnblogs.com/kernel0815/p/3776656.html
Copyright © 2011-2022 走看看