大佬教程收集整理的这篇文章主要介绍了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 <@H_675_12@new> #include <@H_675_12@String> #include <algorithm> #include <limits> #include <iostream> typedef boost::error_info<@H_675_12@struct tag_errmsg,std::@H_675_12@String> errmsg_info; @H_675_12@struct alLOCATIOn_Failed : @H_675_12@public boost::exception,@H_675_12@public std::exception { @H_675_12@const @H_675_12@char *what() @H_675_12@const noexcept { @H_675_12@return "alLOCATIOn Failed"; } }; @H_675_12@char *allocate_memory(std::size_t sizE) { @H_675_12@char *c = @H_675_12@new (std::nothrow) @H_675_12@char[size]; @H_675_12@if (!C) @H_675_12@throw alLOCATIOn_Failed{}; @H_675_12@return c; } @H_675_12@char *write_lots_of_zeros() { @H_675_12@try { @H_675_12@char *c = allocate_memory(std::numeric_limits<std::size_t>::max()); std::fill_n(c,std::numeric_limits<std::size_t>::max(),0); @H_675_12@return c; } @H_675_12@catch (boost::exception &E) { e << errmsg_info{"wriTing lots of zeros Failed"}; @H_675_12@throw; } } @H_675_12@int main() { @H_675_12@try { @H_675_12@char *c = write_lots_of_zeros(); @H_675_12@delete[] c; } @H_675_12@catch (boost::exception &E) { std::cerr << boost::diagnostic_information(E); } @H_675_12@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 @H_684_167@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
@H_675_12@char *allocate_memory(std::size_t sizE) { @H_675_12@char *c = @H_675_12@new (std::nothrow) @H_675_12@char[size]; @H_675_12@if (!C) BOOST_THROW_EXCEPTION(alLOCATIOn_Failed{}); @H_675_12@return c; }
output:
@H_684_148@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 Failedusing 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全部内容,希望文章能够帮你解决boost exception所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。