Boost logo

Boost Users :

Subject: Re: [Boost-users] exception: use of error_info can mask real exceptions
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2009-05-21 14:05:41


On Wed, May 20, 2009 at 8:17 PM, Jon Kalb <Jon_at_[hidden]> wrote:
>> Just wanted to ask if there were commonly accepted solutions to the
>> following problem:
>>
>> typedef boost::error_info<struct tag_f1, std::string> f1_descr;
>>
>> and then
>>
>> throw f1exception() << f1_descr("blah blah blah").
>>
>> and later
>>
>> catch(f1exception & ex){...}
>>
>> The constructor of std::string may throw an exception in which case
>> f1exception will never be thrown.
>> This is not necessarily the desired outcome as the intention was
>> obviously to indicate f1exception error,
>> not the string() error.

Jon Kalb raised this issue at BoostCon. My knee-jerk reaction was that
it makes sense to alter the specification of operator<< so it never
throws, but now my opinion is that it isn't a good idea.

Consider this slightly modified example:

std::string get_descr();

....
throw f1exception() << f1_descr(get_descr());

Even if operator<< and f1_descr's constructor never throw, in theory
you could still get std::bad_alloc instead of f1exception.

Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net