我尝试使用提升异常和下降。有问题代码:
struct exception_base : virtual std::exception, virtual boost::exception
{
exception_base(std::exception&& e)
: std::exception(e)
{}
};
int main()
{
std::string exception_description;
try
{
BOOST_THROW_EXCEPTION(exception_base(std::runtime_error("hello exception")));
}
catch (exception_base& ex)
{
exception_description = boost::diagnostic_information(ex);
}
return 0;
}
在这种情况下, exception_description 的值具有最后一个字符串 - “ std::exception::what: Unknown exception ”。这是意想不到的价值。如果我将 BOOST_THROW_EXCEPTION 更改为通常的抛出 - exception_description 值的最后一个字符串看起来是预期的 - “ std::exception::what: hello exception ”
那么如何正确使用 BOOST_THROW_EXCEPTION 呢?