提问人:Pat 提问时间:1/10/2018 更新时间:1/11/2018 访问量:90
如何遍历boost::exception中的所有error_info?
How to iterate over all error_info's in boost::exception?
问:
在 boost::exception(或 std::exception)的 catch 站点,我想遍历异常的所有error_info元素,而不知道类型。我需要提取所有名称-值对。
我想这应该是可能的,因为boost::d iagnostic_information函数可以做到这一点,但我想避免复制所有这些代码。
这能做到吗,怎么做到?
答:
1赞
sehe
1/11/2018
#1
始终有以下信息(如果您使用过):BOOST_THROW_EXCEPTION
char const * const * f=get_error_info<throw_file>(*be);
int const * l=get_error_info<throw_line>(*be);
char const * const * fn=get_error_info<throw_function>(*be);
if( !f && !l && !fn )
tmp << "Throw location unknown (consider using BOOST_THROW_EXCEPTION)\n";
除此之外,您还使用了 ,但该成员是私有的¹。error_info_container
data_
如果你愿意“强行”越过这个障碍,“复制”的代码就不会那么多了:
char const *
diagnostic_information( char const * header ) const
{
if( header )
{
std::ostringstream tmp;
tmp << header;
for( error_info_map::const_iterator i=info_.begin(),end=info_.end(); i!=end; ++i )
{
error_info_base const & x = *i->second;
tmp << x.name_value_string();
}
tmp.str().swap(diagnostic_info_str_);
}
return diagnostic_info_str_.c_str();
}
那里的所有内容都是未记录的,但不是公共 API 的一部分:它位于命名空间和类中。boost::exception_detail
boost::exception_detail::exception_info_container_impl
简而言之,有龙(这些界面可能会更改,恕不另行通知,并且可能取决于令人惊讶的假设)。
¹(某些较旧的编译器除外)。
评论