【发布时间】:2017-10-30 02:25:20
【问题描述】:
考虑以下示例(取自https://theboostcpplibraries.com/boost.exception)
#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 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)
BOOST_THROW_EXCEPTION(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::get_error_info<errmsg_info>(e);
}
}
函数allocate_memory()使用以下语句抛出异常
BOOST_THROW_EXCEPTION(allocation_failed{});
如何在 catch 块中将 boost::exception &e 转换回 allocation_failed?
另外,如果我的代码有多个 throw 语句,例如 BOOST_THROW_EXCEPTION(A{})、BOOST_THROW_EXCEPTION(B{})、BOOST_THROW_EXCEPTION(C{}) 等。其中 A、B、C 是类。在不使用 boost 的情况下,我可以通过以下方式为每种类型的异常设置单独的 catch 块。
...
catch(A e){
...
}
catch(B e){
...
}
catch(C e){
...
}
如何在使用 boost 时做同样的事情,让 BOOST_THROW_EXCEPTION(A{})、BOOST_THROW_EXCEPTION(B{})、BOOST_THROW_EXCEPTION(C{}) 等进入不同的 catch 块?
我是 boost 库的新手,它的一些概念让我难以理解。
【问题讨论】:
-
为什么要坚持boost? (为什么不使用 RAII?)
-
我正在使用以这种方式引发异常的 API。我将不得不阅读有关 RAII 的信息。过关后会回来的。谢谢。
标签: c++ boost exception-handling