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. php面试专题---16、MySQL创建高性能索引考点

    php面试专题---16.MySQL创建高性能索引考点 一.总结 一句话总结: 注意:只写精品 1.索引的基础? 类似书籍的目录:索引类似于书籍的目录,要想找到一本书的某个特定主题,需要先查找书的目录 ...

  2. ORACLE 收缩表空间的数据文件

    http://blog.itpub.net/29345367/viewspace-1816427/ 方法一: 在实际的应用中经常会遇到TRUNCATE或者DELETE表中的数据后发现表空间并没有将空间 ...

  3. nacos 报错is not in serverlist

    描述 nacos 没有在节点列表里面 查看日志 cd /opt/nacos/ tailf /logs/naming-raft.log <!--报错--> 2019-08-16 17:48: ...

  4. debain8 安装mysql8

    一.下载apt源 https://dev.mysql.com/downloads/repo/apt/ 二.更新apt sudo apt-get update 三.安装mysql sudo apt-ge ...

  5. PHP日期加减函数

    <?phpecho "今天:",date('Y-m-d H:i:s'),"<br>";echo "明天:",date('Y ...

  6. Java连接SQL Server:jTDS驱动兼容性问题

    Java连接SQL Server 2000数据库时,有两种方法: (1)通过Microsoft的JDBC驱动连接.此JDBC驱动共有三个文件,分别是mssqlserver.jar.msutil.jar ...

  7. UML类图及类之间关系

    1.UML基本介绍 UML:统一建模语言,是一种用于软件系统分析和设计的语言工具 2.UML图 UML图分类: 用例图 静态结构图:类图,对象图,包图,组件图,部署图 动态行为图:交互图,状态图,活动 ...

  8. Pandas案例--人口密度分析

    需求: 导入文件,查看原始数据 将人口数据和各州简称数据进行合并 将合并的数据中重复的abbreviation列进行删除 查看存在缺失数据的列 找到有哪些state/region使得state的值为N ...

  9. 56-python基础-python3-集合-新建集合

    集合对象是一组无序排列的可哈希的值,集合成员可以做字典中的键. 集合支持用in和not in操作符检查成员. 由len()内建函数得到集合的基数(大小). 用 for 循环迭代集合的成员. 但是因为集 ...

  10. TensorFlow 安装报错的解决办法

    最近关注了几个python相关的公众号,没事随便翻翻,几天前发现了一个人工智能公开课,闲着没事,点击了报名. 几天都没有音信,我本以为像我这种大龄转行的不会被审核通过,没想到昨天来了审核通过的电话,通 ...