Support for std::nested_exception in Boost.Exception?

Are there any plans (or any desire) for supporting the nested exception functionality (N2559) now in the C++0x draft in the Boost.Exception library? -Chris

On Thu, Nov 20, 2008 at 5:09 AM, Chris Newbold <Chris.Newbold@mathworks.com> wrote:
Are there any plans (or any desire) for supporting the nested exception functionality (N2559) now in the C++0x draft in the Boost.Exception library?
I don't see much value in wrapping or translating the type of exception objects, though I find it quite useful to augment them with additional information as they propagate up the call stack (that's the essence of Boost Exception.) So, the answer is that I personally don't have such plans. :) Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode

From: boost-bounces@lists.boost.org [mailto:boost- bounces@lists.boost.org] On Behalf Of Emil Dotchevski Sent: Monday, November 24, 2008 5:03 PM
I don't see much value in wrapping or translating the type of exception objects, though I find it quite useful to augment them with additional information as they propagate up the call stack (that's the essence of Boost Exception.)
I have found wrapping to be the only viable way to deal with large, heterogeneous systems built atop many libraries from disparate sources which cannot be modified to integrate into a unified exception hierarchy. I agree that the ability to augment exceptions is much cleaner, but when the "original" exception isn't a boost::exception, I need a clean way to bridge it into the world of boost::exceptions. Anyway, std::nested_exception looks pretty trivial to implement given the bits that already exist in Boost.Exception. Would you be open to a patch providing an implementation? -Chris

On Tue, Nov 25, 2008 at 7:41 AM, Chris Newbold <Chris.Newbold@mathworks.com> wrote:
Anyway, std::nested_exception looks pretty trivial to implement given the bits that already exist in Boost.Exception. Would you be open to a patch providing an implementation?
What's wrong with struct whatever { }; struct translated: boost::exception, std::exception { }; typedef boost::error_info<struct tag_nested_exception,boost::exception_ptr> nested_exception; .... catch( whatever & e ) { throw translated() << nested_exception(boost::copy_exception(e)); } Elsewhere, instead of N2559 rethrow_nested: catch( boost::exception & e ) //or catch(translated & e) { if( boost::shared_ptr<boost::exception_ptr const> n=boost::get_error_info<nested_exception>(e) ) rethrow_exception(*n); } Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode

Emil Dotchevski wrote:
On Tue, Nov 25, 2008 at 7:41 AM, Chris Newbold <Chris.Newbold@mathworks.com> wrote:
Anyway, std::nested_exception looks pretty trivial to implement given the bits that already exist in Boost.Exception. Would you be open to a patch providing an implementation?
What's wrong with
struct whatever { }; struct translated: boost::exception, std::exception { };
typedef boost::error_info<struct tag_nested_exception,boost::exception_ptr> nested_exception;
.... catch( whatever & e ) { throw translated() << nested_exception(boost::copy_exception(e)); }
Elsewhere, instead of N2559 rethrow_nested:
catch( boost::exception & e ) //or catch(translated & e) { if( boost::shared_ptr<boost::exception_ptr const> n=boost::get_error_info<nested_exception>(e) ) rethrow_exception(*n); }
You just reimplemented std::nested_exception. After all, nested_exception is a pretty thin wrapper around the exception_ptr mechanism. Sebastian

On Tue, Nov 25, 2008 at 1:58 PM, Sebastian Redl <sebastian.redl@getdesigned.at> wrote:
Emil Dotchevski wrote:
On Tue, Nov 25, 2008 at 7:41 AM, Chris Newbold <Chris.Newbold@mathworks.com> wrote:
Anyway, std::nested_exception looks pretty trivial to implement given the bits that already exist in Boost.Exception. Would you be open to a patch providing an implementation?
What's wrong with
struct whatever { }; struct translated: boost::exception, std::exception { };
typedef boost::error_info<struct tag_nested_exception,boost::exception_ptr> nested_exception;
.... catch( whatever & e ) { throw translated() << nested_exception(boost::copy_exception(e)); }
Elsewhere, instead of N2559 rethrow_nested:
catch( boost::exception & e ) //or catch(translated & e) { if( boost::shared_ptr<boost::exception_ptr const> n=boost::get_error_info<nested_exception>(e) ) rethrow_exception(*n); }
You just reimplemented std::nested_exception. After all, nested_exception is a pretty thin wrapper around the exception_ptr mechanism.
Depends on what you mean by "reimplemented". The code above uses a different interface than N2559. My point was that I don't see why nested exceptions need any special handling in Boost, provided that boost::exception can transport anything at all, including exception_ptr if that's what you want. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode

When Alisdair asked me to comment on the nested_exception paper, I suggested that it needs to be folded into std::exception, providing std::exception_ptr std::exception::context() const; that returns the value of std::current_exception() at construction time. This was (unfairly - IMHO) rejected for ABI reasons. With that in mind, Emil Dotchevski:
What's wrong with
struct whatever { }; struct translated: boost::exception, std::exception { };
typedef boost::error_info<struct tag_nested_exception,boost::exception_ptr> nested_exception;
.... catch( whatever & e ) { throw translated() << nested_exception(boost::copy_exception(e));
I'd say that this needs to be: throw translated() << context( boost::current_exception() ); or, actually: BOOST_THROW_WITH_CONTEXT( translated() );
}
Under C++03 we need to be careful to not call current_exception() when an exception isn't active, so we can't just automatically fold this into the boost::exception constructor or the existing macro. -- Peter Dimov http://www.pdplayer.com

On Tue, Nov 25, 2008 at 4:25 PM, Peter Dimov <pdimov@pdimov.com> wrote:
When Alisdair asked me to comment on the nested_exception paper, I suggested that it needs to be folded into std::exception, providing
std::exception_ptr std::exception::context() const;
that returns the value of std::current_exception() at construction time. This was (unfairly - IMHO) rejected for ABI reasons. With that in mind,
Emil Dotchevski:
What's wrong with
struct whatever { }; struct translated: boost::exception, std::exception { };
typedef boost::error_info<struct tag_nested_exception,boost::exception_ptr> nested_exception;
.... catch( whatever & e ) { throw translated() << nested_exception(boost::copy_exception(e));
I'd say that this needs to be:
throw translated() << context( boost::current_exception() );
The problem is that boost::current_exception may not always be able to get you the correct exception type (so far we only have Antony's implementation for MSVC which I haven't yet integrated in boost::exception, and which doesn't work in 64-bit builds AFAIK.) Certainly, if we're considering some 3rd party library which throws exceptions using throw directly, boost::current_exception won't do -- you need to catch this and that and use boost::copy_exception. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
participants (4)
-
Chris Newbold
-
Emil Dotchevski
-
Peter Dimov
-
Sebastian Redl