[exception] Using Boost.Exception infrastructure without using boost::exception
I'm interested in using the infrastructure in Boost.Exception without using using boost::exception or deriving from it. Is this possible, if so, how? From the documentation, it seems so, but the documentation is not totally clear on this. From what I deduced, one should do: class my_exception {}; //1 throw boost::enable_current_exception(my_exception()); //2 catch(my_exception & ex) { /do stuff .... boost::rethrow_exception(boost::current_exception()); } Though, http://www.boost.org/doc/libs/1_46_1/libs/exception/doc/enable_current_excep... states: Note: Instead of using the throw keyword directly, it is preferable to call boost::throw_exception. This is guaranteed to throw an exception that derives from boost::exception and supports the exception_ptr functionality. And it's not very clear what exactly are the requirements for supporting exception_ptr functionality if one doesn't derive from boost::exception. Thanks, Mostafa
On Sat, May 28, 2011 at 6:27 PM, Mostafa
Though, http://www.boost.org/doc/libs/1_46_1/libs/exception/doc/enable_current_excep... states:
Note: Instead of using the throw keyword directly, it is preferable to call boost::throw_exception. This is guaranteed to throw an exception that derives from boost::exception and supports the exception_ptr functionality.
And it's not very clear what exactly are the requirements for supporting exception_ptr functionality if one doesn't derive from boost::exception.
If you want to use the error info or exception_ptr functionality, the exception being thrown must derive from boost::exception. If you pass an exception object of type T that doesn't derive from boost::exception to boost::throw_exception, it copies it into an object that derives from both boost::exception and T, and throws that object. Or you can use the BOOST_THROW_EXCEPTION macro which in addition will store __FILE__ and __LINE__ in the exception object, giving you more useful boost::diagnostic_information. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
On Sat, 28 May 2011 18:56:23 -0700, Emil Dotchevski
On Sat, May 28, 2011 at 6:27 PM, Mostafa
wrote:
<snip>
If you want to use the error info or exception_ptr functionality, the exception being thrown must derive from boost::exception.
The feature I'm only interested in is using Boost.Exception to enable my exceptions to be "thread-safe" without explicitly or implicitly deriving from boost::exception. It seems that to do so, at a minimum I would need: 1) boost::enable_current_exception 2) boost::rethrow_exception Is this true? boost::rethrow_exception requires exception_ptr and you say the latter requires derivation from boost::exception. So it seems it's not possible to accomplish what I want using the existing Boost.Exception framework. If so, is it possible to add such a feature? Thanks, Mostafa
On Sat, May 28, 2011 at 8:05 PM, Mostafa
On Sat, 28 May 2011 18:56:23 -0700, Emil Dotchevski
wrote: On Sat, May 28, 2011 at 6:27 PM, Mostafa
wrote: <snip>
If you want to use the error info or exception_ptr functionality, the exception being thrown must derive from boost::exception.
The feature I'm only interested in is using Boost.Exception to enable my exceptions to be "thread-safe" without explicitly or implicitly deriving from boost::exception.
How do you define thread safety for exception objects? The only thread safety guarantees provided by Boost Exception relate to boost::exception_ptr objects. All of the Boost Exception functionality works only if the exceptions derive from boost::exception. Your only two choices are to do it explicitly or to call boost::throw_exception which does it for you. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
On Sat, 28 May 2011 20:24:53 -0700, Emil Dotchevski
On Sat, May 28, 2011 at 8:05 PM, Mostafa
wrote: On Sat, 28 May 2011 18:56:23 -0700, Emil Dotchevski
wrote: On Sat, May 28, 2011 at 6:27 PM, Mostafa
wrote: <snip>
If you want to use the error info or exception_ptr functionality, the exception being thrown must derive from boost::exception.
The feature I'm only interested in is using Boost.Exception to enable my exceptions to be "thread-safe" without explicitly or implicitly deriving from boost::exception.
How do you define thread safety for exception objects? The only thread safety guarantees provided by Boost Exception relate to boost::exception_ptr objects.
The ability to transport exceptions between threads (yes, I may have been misusing the term in my previous posting), as is mentioned here: http://www.boost.org/doc/libs/1_46_1/libs/exception/doc/tutorial_exception_p...
All of the Boost Exception functionality works only if the exceptions derive from boost::exception. Your only two choices are to do it explicitly or to call boost::throw_exception which does it for you.
It seems that some functionality doesn't, namely boost::enable_current_exception, and I was hoping to leverage such functionality to achieve my aforementioned goal. Even if it's not possible to fulfill my requirement using the existing tools in Boost.Exception, I still would be interested in knowing if it's possible to expand Boost.Exception to realize such a feature. Thanks, Mostafa
On Sat, May 28, 2011 at 8:37 PM, Mostafa
All of the Boost Exception functionality works only if the exceptions derive from boost::exception. Your only two choices are to do it explicitly or to call boost::throw_exception which does it for you.
It seems that some functionality doesn't, namely boost::enable_current_exception, and I was hoping to leverage such functionality to achieve my aforementioned goal.
boost::enable_current_exception enables the boost::current_exception/boost::exception_ptr support by returning an object that derives from boost::exception *and* the type of the passed object. Therefore: throw boost::enable_current_exception(my_exception()); satisfies the requirement for deriving from boost::exception without requiring my_exception to derive from boost::exception. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
On Sat, 28 May 2011 21:01:56 -0700, Emil Dotchevski
On Sat, May 28, 2011 at 8:37 PM, Mostafa
wrote: All of the Boost Exception functionality works only if the exceptions derive from boost::exception. Your only two choices are to do it explicitly or to call boost::throw_exception which does it for you.
It seems that some functionality doesn't, namely boost::enable_current_exception, and I was hoping to leverage such functionality to achieve my aforementioned goal.
boost::enable_current_exception enables the boost::current_exception/boost::exception_ptr support by returning an object that derives from boost::exception *and* the type of the passed object.
Therefore:
throw boost::enable_current_exception(my_exception());
satisfies the requirement for deriving from boost::exception without requiring my_exception to derive from boost::exception.
If that is so, then that fact is omitted from the documentation for boost::enable_current_exception, please see: http://www.boost.org/doc/libs/1_46_1/libs/exception/doc/enable_current_excep... and the Note at the bottom of the said url seems to imply otherwise, by stating "it is preferable to call boost::throw_exception. This is guaranteed to throw an exception that derives from boost::exception". Can you please add this fact to the above documentation? The documentation for boost::throw_exception is more clear that it derives from the user defined type and boost::exception, see http://tinyurl.com/3ks92g8, where it mentions "... boost::throw_exception(e) is equivalent to throw boost::enable_current_exception(boost::enable_error_info(e)) ...", and the documentation for boost::enable_error_info (http://tinyurl.com/3zo3yu7) makes it explicit that "... the returned object is of an unspecified type that derives publicly from both T and boost::exception.". Hence, the reason for my confusion on this matter. Thanks, Mostafa
On Sat, 28 May 2011 21:50:44 -0700, Mostafa
On Sat, 28 May 2011 21:01:56 -0700, Emil Dotchevski
wrote: On Sat, May 28, 2011 at 8:37 PM, Mostafa
wrote: All of the Boost Exception functionality works only if the exceptions derive from boost::exception. Your only two choices are to do it explicitly or to call boost::throw_exception which does it for you.
It seems that some functionality doesn't, namely boost::enable_current_exception, and I was hoping to leverage such functionality to achieve my aforementioned goal.
boost::enable_current_exception enables the boost::current_exception/boost::exception_ptr support by returning an object that derives from boost::exception *and* the type of the passed object.
Therefore:
throw boost::enable_current_exception(my_exception());
satisfies the requirement for deriving from boost::exception without requiring my_exception to derive from boost::exception.
If that is so, then that fact is omitted from the documentation for boost::enable_current_exception, please see:
http://www.boost.org/doc/libs/1_46_1/libs/exception/doc/enable_current_excep...
and the Note at the bottom of the said url seems to imply otherwise, by stating "it is preferable to call boost::throw_exception. This is guaranteed to throw an exception that derives from boost::exception". Can you please add this fact to the above documentation?
The documentation for boost::throw_exception is more clear that it derives from the user defined type and boost::exception, see http://tinyurl.com/3ks92g8, where it mentions "... boost::throw_exception(e) is equivalent to throw boost::enable_current_exception(boost::enable_error_info(e)) ...", and the documentation for boost::enable_error_info (http://tinyurl.com/3zo3yu7) makes it explicit that "... the returned object is of an unspecified type that derives publicly from both T and boost::exception.". Hence, the reason for my confusion on this matter.
Thanks,
Mostafa
Correction: s/"Can you please add this fact to the above documentation?"/"Can you please add what you just mentioned to the above documentation?" Thanks, Mostafa
On Sat, May 28, 2011 at 9:50 PM, Mostafa
On Sat, 28 May 2011 21:01:56 -0700, Emil Dotchevski
wrote: On Sat, May 28, 2011 at 8:37 PM, Mostafa
wrote: All of the Boost Exception functionality works only if the exceptions derive from boost::exception. Your only two choices are to do it explicitly or to call boost::throw_exception which does it for you.
It seems that some functionality doesn't, namely boost::enable_current_exception, and I was hoping to leverage such functionality to achieve my aforementioned goal.
boost::enable_current_exception enables the boost::current_exception/boost::exception_ptr support by returning an object that derives from boost::exception *and* the type of the passed object.
Therefore:
throw boost::enable_current_exception(my_exception());
satisfies the requirement for deriving from boost::exception without requiring my_exception to derive from boost::exception.
If that is so, then that fact is omitted from the documentation for boost::enable_current_exception, please see:
Ah right, my mistake! It returns a type that derives from an unspecified type and T. The documentation is correct. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
On Sat, 28 May 2011 21:57:34 -0700, Emil Dotchevski
On Sat, May 28, 2011 at 9:50 PM, Mostafa
wrote: On Sat, 28 May 2011 21:01:56 -0700, Emil Dotchevski
wrote: On Sat, May 28, 2011 at 8:37 PM, Mostafa
wrote: All of the Boost Exception functionality works only if the exceptions derive from boost::exception. Your only two choices are to do it explicitly or to call boost::throw_exception which does it for you.
It seems that some functionality doesn't, namely boost::enable_current_exception, and I was hoping to leverage such functionality to achieve my aforementioned goal.
boost::enable_current_exception enables the boost::current_exception/boost::exception_ptr support by returning an object that derives from boost::exception *and* the type of the passed object.
Therefore:
throw boost::enable_current_exception(my_exception());
satisfies the requirement for deriving from boost::exception without requiring my_exception to derive from boost::exception.
If that is so, then that fact is omitted from the documentation for boost::enable_current_exception, please see:
Ah right, my mistake! It returns a type that derives from an unspecified type and T. The documentation is correct.
I do try to do my homework before posting here. :) Now, back to the original purpose of my postings: My Requirements: ---------------- To use Boost.Exception tools to make my exceptions transportable between threads without explicitly or implicitly deriving from Boost.Exception. So it seems I'm halfway there with "throw boost::enable_current_exception(my_exception());". Now I would like to rethrow from some catch site, it is my understanding then that I would need to use boost::rethrow_exception() (in order to stay within the parameters of my requirements). But boost::rethrow_exception requires a boost::exception_ptr reference (which seems can only be gotten via boost::current_exception). Now, does using "throw boost::enable_current_exception(my_exception());" with class my_exception which does not derive from boost::exception support the exception_ptr functionality that is need for boost::rethrow_exception? Thanks, Mostafa
On Sat, May 28, 2011 at 10:21 PM, Mostafa
My Requirements: ---------------- To use Boost.Exception tools to make my exceptions transportable between threads without explicitly or implicitly deriving from Boost.Exception.
So it seems I'm halfway there with "throw boost::enable_current_exception(my_exception());". Now I would like to rethrow from some catch site, it is my understanding then that I would need to use boost::rethrow_exception() (in order to stay within the parameters of my requirements). But boost::rethrow_exception requires a boost::exception_ptr reference (which seems can only be gotten via boost::current_exception). Now, does using "throw boost::enable_current_exception(my_exception());" with class my_exception which does not derive from boost::exception support the exception_ptr functionality that is need for boost::rethrow_exception?
Yes. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
On Sat, 28 May 2011 22:25:08 -0700, Emil Dotchevski
On Sat, May 28, 2011 at 10:21 PM, Mostafa
wrote: My Requirements: ---------------- To use Boost.Exception tools to make my exceptions transportable between threads without explicitly or implicitly deriving from Boost.Exception.
So it seems I'm halfway there with "throw boost::enable_current_exception(my_exception());". Now I would like to rethrow from some catch site, it is my understanding then that I would need to use boost::rethrow_exception() (in order to stay within the parameters of my requirements). But boost::rethrow_exception requires a boost::exception_ptr reference (which seems can only be gotten via boost::current_exception). Now, does using "throw boost::enable_current_exception(my_exception());" with class my_exception which does not derive from boost::exception support the exception_ptr functionality that is need for boost::rethrow_exception?
Yes.
Thank you. Now, may the above fact be added as a special section to Boost.Exception documentation so others don't have to go through the same long-winded discussion that I did? And the reason for my caution and me posting here was the Note at the bottom of the documentation for boost::enable_current_exception (see http://tinyurl.com/3tqsqw6): "Note: Instead of using the throw keyword directly, it is preferable to call boost::throw_exception. This is guaranteed to throw an exception that derives from boost::exception and supports the exception_ptr functionality." Which to the casual reader implies that boost::enable_current_exception does not support the boost::exception_ptr functionality. Maybe taking out "and supports the exception_ptr functionality" or explicitly stating that boost::enable_current_exception also supports such functionality will alleviate any confusion or potential confusion on the part of the users. Thanks, Mostafa
participants (2)
-
Emil Dotchevski
-
Mostafa