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(), );
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 ;
}

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.

boost exception的更多相关文章

  1. boost exception jam0.exe 异常错误

    在Windows 8 64 bit下执行boost_1_53_0的bootstrap.bat出现了jam0.exe执行错误 搜索网页发现需要修改两处文件: tools/build/v2/engine/ ...

  2. boost:exception使用实例

    /************************************************************************/ /*功能描述: boost exception使用 ...

  3. Win7下Boost库的安装

    Boost库是C++领域公认的经过千锤百炼的知名C++类库,涉及编程中的方方面面,简单记录一下使用时的安装过程 1.boost库的下载 boost库官网主页:www.boost.org 2.安装 将下 ...

  4. (八)boost库之异常处理

    (八)boost库之异常处理 当你面对上千万行的项目时,当看到系统输出了异常信息时,你是否想过,如果它能将文件名.行号等信息输出,该多好啊,曾经为此绞尽脑汁. 今天使用boost库,将轻松的解决这个问 ...

  5. Boost 1.61.0 Library Documentation

    http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for ...

  6. 使用Boost program_options控制程序输入

    简介 很多人使用界面来输入数据,本文的程序介绍如何使用Boost的program_options控制输入. 程序中使用了: 1. 短选项 2. 可以指定多个参数的选项 程序 #include < ...

  7. boost::coroutine 无法显示调用栈

    boost::coroutine 无法显示调用栈(金庆的专栏)一例因 boost::format() 格式化参数个数错误造成的 coredump,因为使用了 boost::coroutine, 无法显 ...

  8. boost库之udp广播实例

    //UdpLinkServer.h //udp服务 #pragma once #include <boost/asio/ip/tcp.hpp> #include <boost/asi ...

  9. 在IDE中集成boost

    1. 获得Boost 进入Boost的网站(http://www.boost.org/) 下载boost_1_62_0.zip 2. 解压Boost 解压 boost_1_62_0.zip ,比如解压 ...

随机推荐

  1. HDU6719 Strassen(__int128)

    HDU6719 Strassen 直接照题目模拟,数据范围最大不会超过__int128. 时间复杂度为 \(O(\log n)\) . #include<bits/stdc++.h> us ...

  2. QQ空间分享网址

    现在大部分网站都在每个界面设计了分享这个功能,但还是有的网页没有(比如 B 站只能分享具体的视频).在原来的 QQ 空间分享的地方已经找不到法自己创建分享.上网一搜有分享的接口,可这个接口是给开发者用 ...

  3. 2019年Java Web最流行的开发框架总结

    ORM型框架:对数据进行持久化操作,例如:基于SQL的MyBatis框架和Hibernate框架. MVC型框架:从逻辑上分为视图层,控制层,模型层,各层各司其职,之间是相互调用的关系,而不是相互依赖 ...

  4. Gradient Descent with Momentum and Nesterov Momentum

    在Batch Gradient Descent及Mini-batch Gradient Descent, Stochastic Gradient Descent(SGD)算法中,每一步优化相对于之前的 ...

  5. 函数式编程filter和map的区别

    # b = filter(lambda x:x>5,[1,2,3,4,5,6,7]) # print(list(b)) def filters(x): if x > 5: return x ...

  6. IDF-CTF-cookie欺骗 writeup

    题目链接: http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=40 知识点:base64解码, cookie欺骗 ...

  7. python的包

    1. 无论是import形式还是from...import形式,凡是在导入语句中(而不是在使用时)遇到带点的,都要第一时间提高警觉:这是关于包才有的导入语法 2. 包是目录级的(文件夹级),文件夹是用 ...

  8. _groovy

    _groovy与beanshell类似,只是它执行的是apache groovy脚本,并返回结果. 如果定义了属性 “groovy.utilities”,属性将会被脚本引擎加载,这样就可以定义一些通用 ...

  9. Spring切面编程之AOP

    AOP 是OOP 的延续,是Aspect Oriented Programming 的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种 ...

  10. CentOS安装ruby, Haskall,io语言

    安装ruby yum install ruby irb rdoc 安装Haskall yum install ghc 安装io语言 安装io语言,需要先安装cmake不过不要使用yum来进行安装,yu ...