[exception] boost::throw_exception vs deriving from boost::exception

I'd like to use Boost.Exception's error_info capabilities in an existing code base. It seems to me that I have two choices: 1 - find all the 'throw my_except(...)' in the code base and replace them with boost::throw_exception(my_except(...)). 2 - find all the struct my_except{...} definitions in the current code base and replace them with: struct my_except: virtual boost::exception {...} I find #2 more appealing for the following practical reasons: - there are fewer locations to change. Exceptions are defined less often and in fewer places than they are used. - there is less to remember. Developers who write new throw expressions need to remember nothing. Developers who define new exceptions must remember to derive them from boost::exception, but this is much less common. Are there reasons I should prefer boost::throw_exception instead? I can only think of one: boost::throw_exception also adds N2179 support via enable_current_exception. This isn't a current concern, but our compilers (gcc and clang) already support N2179 directly, so we can use std::current_exception rather than boost::current_exception if the need arises. Are there other reasons to prefer boost::throw_exception? Thanks John Salmon

On Sun, Oct 7, 2012 at 9:29 AM, John Salmon <john@thesalmons.org> wrote:
I'd like to use Boost.Exception's error_info capabilities in an existing code base. It seems to me that I have two choices:
1 - find all the 'throw my_except(...)' in the code base and replace them with boost::throw_exception(my_except(...)).
2 - find all the struct my_except{...} definitions in the current code base and replace them with: struct my_except: virtual boost::exception {...}
I find #2 more appealing for the following practical reasons:
- there are fewer locations to change. Exceptions are defined less often and in fewer places than they are used.
- there is less to remember. Developers who write new throw expressions need to remember nothing. Developers who define new exceptions must remember to derive them from boost::exception, but this is much less common.
Are there reasons I should prefer boost::throw_exception instead? I can only think of one: boost::throw_exception also adds N2179 support via enable_current_exception. This isn't a current concern, but our compilers (gcc and clang) already support N2179 directly, so we can use std::current_exception rather than boost::current_exception if the need arises.
Are there other reasons to prefer boost::throw_exception?
I think it also allows you to disable exceptions (rather than throw, call a user-defined exception handler and terminate). Other than that and enable_current_exception, I don't think there's anything else that throw_exception provides over throw. - Jeff

On Sun, Oct 7, 2012 at 9:34 AM, Jeffrey Lee Hellrung, Jr. <jeffrey.hellrung@gmail.com> wrote:
On Sun, Oct 7, 2012 at 9:29 AM, John Salmon <john@thesalmons.org> wrote:
I'd like to use Boost.Exception's error_info capabilities in an existing code base. It seems to me that I have two choices:
1 - find all the 'throw my_except(...)' in the code base and replace them with boost::throw_exception(my_except(...)).
2 - find all the struct my_except{...} definitions in the current code base and replace them with: struct my_except: virtual boost::exception {...}
I find #2 more appealing for the following practical reasons:
If your code base has a single exception type hierarchy, you can add boost::exception as a base of its base type, like it is commonly done with std::exception. That's more robust than specifying it as an added virtual base of the individual exception types.
Are there reasons I should prefer boost::throw_exception instead?
In my own code I use a macro similar to BOOST_THROW_EXCEPTION to throw exceptions. This lets me capture the throw location and store it in the exception object, which helps track down unhandled exceptions.
can only think of one: boost::throw_exception also adds N2179 support via enable_current_exception. This isn't a current concern, but our compilers (gcc and clang) already support N2179 directly, so we can use std::current_exception rather than boost::current_exception if the need arises.
Are there other reasons to prefer boost::throw_exception?
No. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
participants (3)
-
Emil Dotchevski
-
Jeffrey Lee Hellrung, Jr.
-
John Salmon