Boost logo

Boost Users :

Subject: Re: [Boost-users] exception: use of error_info can mask real exceptions
From: Jon Kalb (Jon_at_[hidden])
Date: 2009-05-20 23:17:17


> 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.
>
> This problem pertains to any type that throws exceptions in its
> constructor and is used with error_info idiom.
>
> What may be more appropriate is something like this
>
> typedef boost::error_info<struct tag_f1, shared_pointer<string> >
> f1_descr
> ...
> error condition is detected
> shared_ptr<string> pErrStr;
> try
> {
> pErrStr = new string("Error description");
> }
> catch(bad_alloc & ex)
> {
> }
>
> throw f1exception() << f1_descr(pErrStr);
>
> Meaning that if string fails to allocate memory, I'm willing to forgo
> the
> error description but still throw the intended exception.
>
>
> Is there a cleaner way to do that?
>
> Thanks,
> Andy.

I spoke with Emil about this at BoostCon. He understood and said that he
would be looking at making this the default behavior.

Jon


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