1

At the catch site of a boost::exception (or std::exception), I want to iterate over all error_info elements of the exception, without knowing the types. I need to extract all the name-value pairs.

I guess it should possible since the boost::diagnostic_information function does that, but I'd like to avoid duplicating all that code.

Can this be done and how?

Pat
  • 1,726
  • 11
  • 18

1 Answers1

1

There's always the following information (iff you used 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";

Other than that you have use the error_info_container, but the data_ member is private¹.

If you are willing to "force" past that hurdle, the code to "duplicate" would not be so much:

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

Everything there is undocumented, not part of the public API though: it lives in namespace boost::exception_detail and the class boost::exception_detail::exception_info_container_impl.

In short, there be dragons (these interfaces are subject to change without notice and can depend on surprising assumptions).


¹ (except on some older compilers).

sehe
  • 374,641
  • 47
  • 450
  • 633