boostorg / exception

Boost.org exception module
http://boost.org/libs/exception
Boost Software License 1.0
15 stars 46 forks source link

std::exception_ptr integration doesn't seem to work correctly when the exception is derived from boost::exception #42

Closed pdimov closed 2 years ago

pdimov commented 2 years ago

See https://godbolt.org/z/dofa6bbcc.

This exception type

class my_exception: public std::exception
{
};

can be caught with current_exception and rethrown properly by rethrow_exception:

    try
    {
        throw my_exception();
    }
    catch( ... )
    {
        boost::exception_ptr p = boost::current_exception();

        BOOST_TEST_THROWS( boost::rethrow_exception( p ), my_exception );
    }

However if we add a boost::exception base,

class my_exception2: public std::exception, public boost::exception
{
};

rethrow_exception no longer throws my_exception2:

    try
    {
        throw my_exception2();
    }
    catch( ... )
    {
        boost::exception_ptr p = boost::current_exception();

        BOOST_TEST_THROWS( boost::rethrow_exception( p ), my_exception2 );
        BOOST_TEST_THROWS( boost::rethrow_exception( p ), boost::exception );
    }

even though it does throw something derived from boost::exception:

example.cpp(33): expression 'boost::rethrow_exception( p )' did not throw exception 'my_exception2' in function 'int main()'
1 error detected.
pdimov commented 2 years ago

Same problem exists with exceptions derived from some other standard exception type, such as std::logic_error: https://godbolt.org/z/e73Moo5MP

It looks like the code starting here: https://github.com/boostorg/exception/blob/b8e9e98b33a18e020e0452497a47ca383e0a616d/include/boost/exception/detail/exception_ptr.hpp#L379

isn't correct; std::current_exception() should be used first, when available, not as a last resort.

pdimov commented 2 years ago

See #43.