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.
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 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 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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 如何在swift中暂停和恢复NSTimer.scheduledTimerWithTimeIn
- c – 如何使boost :: iostream以与std :: ios :: binary相当
- c# – 关于班级设计的面试问题
- ruby-on-rails – Rails 4.0.0,Gemfile需要rake 10.1.0 for
- vue的一个分页组件的示例代码
- xml – 选择祖先的Xpath
- cocos2d-x 3.3 Sprite3D人物换装代码解析
- KVM――复制xml文件与磁盘文件克隆虚拟机
- Oracle Grid Infrastructure: Understanding Split-Brain N
- Ruby真的可以用作功能语言吗?