Boost logo

Boost :

Subject: Re: [boost] Boost and exceptions
From: Agustín K-ballo Bergé (kaballo86_at_[hidden])
Date: 2012-06-23 16:37:47


On 23/06/2012 18:25, Robert Ramey wrote:
> Steven Watanabe wrote:
>>
>> Actually, inheriting from boost::exception doesn't
>> work for this. To enable copying you have to throw
>> using boost::enable_current_exception.
>
>> try {
>> throw enable_current_exception(x);
>> } catch(...) {
>> exception_ptr e = current_exception();
>> }
>> // move e to another thread
>> rethrow_exception(e);
>
> OK - get it - but I don't see that it adds any value compared to
>
> try {
> throw x;
> }
> catch(...){
> exception_ptr e = std::current_exception();
> std::rethrow_exception(e); // n3242 18.8}
> }

Would you happen to know which compilers implement n3242 already?
Boost.Exception works for me with the compilers I use everyday, as long
as the original exception was thrown with enable_current_exception.

>> The code using Boost.Exception would look like:
>
>> typedef boost::error_info<
>> struct tag_archive_filename, std::string> archive_filename;
>> ...
>>
>> try {
>> ifstream is(filename);
>> binary_archive ia(is);
>> my_data d;
>> ia >> d;
>> } catch (boost::exception& e) {
>> e << archive_filename(filename);
>> throw;
>> }
>
> lol - now you've thrown away all the information in archive_exception!
> Use this instead.

Most certainly not, the **type** of the original exception is not lost
when using boost::throw_exception. In this case, e is still an
archive_exception.

>>> If you really like
>>> boost::exception
>>> your code when you eventually can eventually rethrow
>>>
>>> catch (std::exception e){
>>> BOOST_EXCEPTION_THROW_EXCEPTION(e)
>>> }
>>>
>>
>> This of course loses the original type of the exception.
>
> I don't think that's true. It's not clear to me from the documentation.
> I get this idea from looking at the code. Of course if it
> were true it would be huge blunder in the design of the library.

Wouldn't that just throw a sliced copy of the exception, a new exception
of type std::exception, instead of rethrowing the original exception?

Agustín K-ballo Bergé.-


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk