Boost logo

Boost :

From: Alexander Nasonov (alnsn_at_[hidden])
Date: 2006-08-08 15:29:20

Oleg Abrosimov wrote:
> Hello Jeremy!
> There is a strong opinion against catch(...) clause. But problems,
> mentioned by Alexander Nasonov and Ames Andreas could be worked around.
> 1) define a BOOST_BROKEN_CATCH_ALL configuration macro that is non-zero
> when compiling in mode where catch(...) is dangerous (BOOST_MSVC is
> defined and _MSC_VER is in a predefined range. according to Peter Dimov
> MSVC 8 doesn't catch SEH exceptions with ... by default)
> 2) protect all code in your catcher.hpp with this macro and place #error
> message in #else branch that describes the problem with catch(...) on a
> given platform. Later it can be replaced with macroses, proposed by
> Alexander Nasonov:
> try {
> throw std::logic_error("testing");
> } BOOST_CATCH_PP_SEQ((ex1)(ex2)(ex3), my_exception_handler());

Does proposed workaround have uniform behavior on all platforms?

> alternatively or additionally, you can provide here an implementation
> that catches not for ... but for std::exception&
> It completely eliminates the original problem but restricts user to only
> std::exception - derived exceptions.
> to extend this approach a bit you can provide user a possibility to
> predefined limit. and use these macros to catch (BOOST_EXCEPTION_BASE_N)
> for all N defined by user.
> Alexander? Ames? What do you prefer here?

When I jumped in into this discussion, I didn't intend to produce new
boost library as I didn't like use of rethrow.
I believe that exception handling should be very simple and not require
a lot of metaprogramming like sorting handlers, creating MPL sequence
of handled exception on the fly and so on.

So, this library should a simple utility library that doesn't rethow if
possible and doesn't hide a lot from a user.

1. Catch a list of exceptions in one line:

    try {
          throw std::logic_error("testing");
    } BOOST_CATCH_PP_SEQ((ex1)(ex2)(ex3), my_exception_handler());

There is no need for MPL counterpart because you can always transform PP
sequence into MPL sequence (but this functionaly seems to be missing in
boost). If you can't do that (e.g. you need to mpl::push_back, or apply
mpl::transform_view), then this library goes beyond a simple utility ;-)

2. Catch and dispatch (or visit, if you like)

   try {
          throw std::logic_error("testing");
   } BOOST_CATCH_AND_DISPATCH(std::exception&, my_exception_handler());

This code rethows but it doesn't catch(...) but rather

Needless to say that my_exception_handler should be as simple as
possible. Macros inside this class should be avoided because it's often
impossible to go to a definition of a function inside it.

Currently, my_exception_handler in item 1 has different (and much
simpler) interface compared to that from item 2. It would be nice
to have common interface.

Alexander Nasonov
Project Manager at Akmosoft ( )
Email: $(FirstName) dot $(LastName) at gmail dot com

Boost list run by bdawes at, gregod at, cpdaniel at, john at