boost::rethrow_exception相关的演示程序
在C++中,抛出异常是一种通用且常见的错误处理方式。然而,在处理异常时有时需要重新抛出异常,以便在更高层次的代码中处理它们。boost库提供了一个非常方便的函数boost::rethrow_exception()来实现这个功能。
boost::rethrow_exception()函数以以下方式定义:
template<typename E>
void boost::rethrow_exception(E const & e);
这个函数将引用一个异常对象,并将其重新抛出。下面的示例演示了如何使用boost::rethrow_exception()函数来重新抛出异常。
#include <iostream>
#include <stdexcept>
#include <boost/exception/all.hpp>
void foo()
{
try
{
throw std::runtime_error("foo failed");
}
catch(...)
{
boost::throw_exception(boost::enable_error_info(std::current_exception())
<< boo