Boost logo

Boost Users :

Subject: [Boost-users] Exception: adding diagnostic information from a dtor?
From: Michael Schuerig (michael.lists_at_[hidden])
Date: 2011-06-14 17:45:14

The FAQ for boost exception contain this example snippet for adding
further information to an exception while the call stack is unwound

catch( boost::exception & e )
    e << foo_info(foo);
    throw; //Okay, re-throwing the original exception object.

I like that this possible, but I think the resulting code is somewhat

void foo(const string& file_name) {
    try {
    } catch( boost::exception & e ) {
        e << boost::errinfo_file_name(file_name);

I'd rather write it like this:

void foo(const string& file_name) {

My first, non-generic attempt fails:

class diagnostic {
    explicit diagnostic(const string& file_name)
        : file_name_(file_name) {}
    ~diagnostic() {
        if ( boost::exception_ptr e = boost::current_exception() ) {
             *e << boost::errinfo_file_name(file_name_.c_str());
    const string file_name_;

The requirements for boost::current_exception() state that it may only
be called inside a catch block, therefore the code above is illegal to
begin with. Ignoring that, operator<< doesn't appear to be suitably
overloaded for this use, anyway.

Now, I'm wondering, is what I'm trying to achieve a bad idea? If not, is
there another way to do it?


Michael Schuerig

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at