c++ - 如何遍历 boost::exception 中的所有 error_info?

标签 c++ boost exception boost-exception

在 boost::exception(或 std::exception)的捕获站点,我想遍历异常的所有 error_info 元素,而不知道类型。我需要提取所有名称-值对。

我想这应该是可能的,因为 boost::diagnostic_information 函数可以做到这一点,但我想避免重复所有这些代码。

这可以做到吗?如何做到?

最佳答案

始终存在以下信息(如果您使用了 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_ 成员是私有(private)的¹。

如果你愿意“强行”越过这个障碍,“复制”的代码就不会那么多了:

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();
    }

这里的一切都没有记录,但不是公共(public) API 的一部分:它位于命名空间 boost::exception_detail 和类 boost::exception_detail::exception_info_container_impl 中。

简而言之,有龙(这些界面如有更改,恕不另行通知,并且可能取决于令人惊讶的假设)。


¹(某些较旧的编译器除外)。

关于c++ - 如何遍历 boost::exception 中的所有 error_info?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48191012/

相关文章:

c++ - 此 shared_ptr 示例中的内容不清楚

python - 为什么有时用finally来调试呢?

c++ - 在同一个应用程序中使用 float 和双重支持编译 kiss fft

c++ - 使用 winapi 或 mfc 从 .NET 应用程序列表控件中获取项目名称

c++ - boost::asio 多线程异步接受阻塞读/写服务器

java - 抛出 Java 异常时是否生成堆栈跟踪?

.net - 在使用 IronPython 和 C#/.NET 作为主机时,如何在运行时错误中获取有问题的行?

c++ - new(size, value) Type[0] 返回的指针是否合法,是否可以用于构建数组?

android - 如何在 opencv C++ 中将图像设为黑白?

c++ - 需要指针地址的 shared_ptr 的自定义删除器