zoukankan      html  css  js  c++  java
  • boost exception

    boost exception provides a new exception type, that lets you add data to an exception after it has been thrown.

    #include <boost/exception/all.hpp>
    #include <exception>
    #include <new>
    #include <string>
    #include <algorithm>
    #include <limits>
    #include <iostream>
    
    typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info;
    
    struct allocation_failed : public boost::exception, public std::exception
    {
      const char *what() const noexcept { return "allocation failed"; }
    };
    
    char *allocate_memory(std::size_t size)
    {
      char *c = new (std::nothrow) char[size];
      if (!c)
        throw allocation_failed{};
      return c;
    }
    
    char *write_lots_of_zeros()
    {
      try
      {
        char *c = allocate_memory(std::numeric_limits<std::size_t>::max());
        std::fill_n(c, std::numeric_limits<std::size_t>::max(), 0);
        return c;
      }
      catch (boost::exception &e)
      {
        e << errmsg_info{"writing lots of zeros failed"};
        throw;
      }
    }
    
    int main()
    {
      try
      {
        char *c = write_lots_of_zeros();
        delete[] c;
      }
      catch (boost::exception &e)
      {
        std::cerr << boost::diagnostic_information(e);
      }
      return 0;
    }

    output:

    Throw location unknown (consider using BOOST_THROW_EXCEPTION)
    Dynamic exception type: struct allocation_failed
    std::exception::what: allocation failed
    [struct tag_errmsg *] = writing lots of zeros failed

    unction write_lots_of_zeros() calls allocate_memory(). allocate_memory() allocates memory dynamically. The function passes std::nothrow to new and checks whether the return values is 0. if memory allocation fails, an exception of type allocation_failed is thrown.

    write_lots_of_zeros() calls allocate_memory() to try and allocate a memory block with the greatest possible size. This is done with the help of max() from std::numeric_limits. The example intentionally tries to allocate that much memory to make the allocation fail.

    With Boost.Exception, data can be added to an exception at any time. You just need to define a type based on boost::error_info for each bit of data you need to add.

    boost::error_info is a template that expects two parameters. The first parameter is a atag that uniquely identifies the newly created type. This is typically a structure with a unique name. The second parameter refers to the type of the value stored inside the exception.

    In the catch handler of write_lots_of_zeros(), errmsg_info is used to create an object that is initialized with the string writing lots of zeros failed”. This object is then added to the exception of type boost::exception using operator<<. Then the exception is re-thrown.

    2. BOOST_THROW_EXCEPTION

    char *allocate_memory(std::size_t size)
    {
      char *c = new (std::nothrow) char[size];
      if (!c)
        BOOST_THROW_EXCEPTION(allocation_failed{});
      return c;
    }

    output:

    main.cpp(20): Throw in function char *__cdecl allocate_memory(unsigned int)
    Dynamic exception type: class boost::exception_detail::clone_impl<struct boost::exception_detail::error_info_injector<struct allocation_failed> >
    std::exception::what: allocation failed
    [struct tag_errmsg *] = writing lots of zeros failed

    using the macro BOOST_THROW_EXCEPTION instead of throw, data such as function name, file name, and line number are automatically added to the exception.

    BOOST_THROW_EXCEPTION accesses the function boost::enable_error_info(), which identifies whether or not an exception is derived from boost::exception. If not, it creates a new exception type derived from the specified type and boost::exception.

  • 相关阅读:
    为什么DIY报价----走出软件作坊:三五个人十来条枪 如何成为开发正规军(十二)[转]
    物以类聚,人以群分--走出软件作坊:三五个人十来条枪 如何成为开发正规军(十一)[转]
    将服务费用DIY到底----走出软件作坊:三五个人十来条枪 如何成为开发正规军(十)[转]
    实施费用也能DIY--走出软件作坊:三五个人十来条枪 如何成为开发正规军(九)[转]
    去掉iphone手机滑动默认行为
    获取json对象长度的问题
    手机淘宝用JS来动态写meta标签(1像素边框处理方法)
    移动端多行文字截断
    移动端初始化页面
    JavaScript 高级程序设计 02-变量、数据类型
  • 原文地址:https://www.cnblogs.com/sssblog/p/11335386.html
Copyright © 2011-2022 走看看