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
tedious:
void foo(const string& file_name) {
try {
do_something_fallible(file_name);
} catch( boost::exception & e ) {
e << boost::errinfo_file_name(file_name);
throw;
}
}
I'd rather write it like this:
void foo(const string& file_name) {
diagnostic(file_name);
do_something_fallible(file_name);
}
My first, non-generic attempt fails:
class diagnostic {
public:
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());
}
}
private:
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
--
Michael Schuerig
mailto:michael@schuerig.de
http://www.schuerig.de/michael/