mirror of
https://github.com/boostorg/exception.git
synced 2025-07-26 02:27:15 +02:00
* Attempt to solve issue #10 In boost::current_exception(), after all supported types of exception have been checked for, a boost::unknown_exception is stored in the boost::exception_ptr as a last resort to signal that an exception was indeed stored, but that the actual type and value of the exception was lost. Now, in case C++11 std::current_exception() is supported, the std::exception_ptr result of calling std::current_exception() is what gets stored inside the boost::exception_ptr. Later, inside boost::rethrow_exception, the std::exception_ptr is retrieved from the boost::exception_ptr and whatever exception it stores is thrown via std::rethrow_exception(). The main benefit is than now any exception thrown via plain 'throw' can be trasnported via boost::exception_ptr. Before this change it was required that the throw site either used boost::enable_current_exception() or boost::throw_exception() or threw an exception type inheriting from boost::exception, which was impossible for third party software that does not use Boost.Exception. The detection of std::current_exception() is currently done via config macro BOOST_NO_CXX11_NOEXCEPT, assuming that if 'noexcept' is supported then std::exception_ptr is also supported. A better solution would require a new dedicated macro in Boost.Config. * Detect support for std::current_exception() via config macro BOOST_NO_CXX11_HDR_EXCEPTION The temporary solution via BOOST_NO_CXX11_NOEXCEPT was an ugly hack that is no longer necessary now that Boost.Config has BOOST_NO_CXX11_HDR_EXCEPTION (pending merge to develop). * Attempt to solve issue #10 In boost::current_exception(), after all supported types of exception have been checked for, a boost::unknown_exception is stored in the boost::exception_ptr as a last resort to signal that an exception was indeed stored, but that the actual type and value of the exception was lost. Now, in case C++11 std::current_exception() is supported, the std::exception_ptr result of calling std::current_exception() is what gets stored inside the boost::exception_ptr. Later, inside boost::rethrow_exception, the std::exception_ptr is retrieved from the boost::exception_ptr and whatever exception it stores is thrown via std::rethrow_exception(). The main benefit is than now any exception thrown via plain 'throw' can be trasnported via boost::exception_ptr. Before this change it was required that the throw site either used boost::enable_current_exception() or boost::throw_exception() or threw an exception type inheriting from boost::exception, which was impossible for third party software that does not use Boost.Exception. The detection of std::current_exception() is currently done via config macro BOOST_NO_CXX11_NOEXCEPT, assuming that if 'noexcept' is supported then std::exception_ptr is also supported. A better solution would require a new dedicated macro in Boost.Config. * Detect support for std::current_exception() via config macro BOOST_NO_CXX11_HDR_EXCEPTION The temporary solution via BOOST_NO_CXX11_NOEXCEPT was an ugly hack that is no longer necessary now that Boost.Config has BOOST_NO_CXX11_HDR_EXCEPTION (pending merge to develop). * Document detection of C++11 std::current_exception through BOOST_NO_CXX11_HDR_EXCEPTION * Added dist: trusty for Travis config, as recommended by pdimov in issue #10 * Stupid gcc 4.7 and 4.8 are confused initializing a reference with braces, using equal sign hoping it works (tested similar code on godbolt).
This commit is contained in:
committed by
GitHub
parent
f68cd7b2d4
commit
af98afb3d1
@ -38,7 +38,7 @@ boost
|
|||||||
<p>Nothing.</p>
|
<p>Nothing.</p>
|
||||||
<h4>Notes:</h4>
|
<h4>Notes:</h4>
|
||||||
<div><ul><li> It is unspecified whether the return values of two successive calls to <span class="RenoLink">current_exception</span> refer to the same exception object.</li>
|
<div><ul><li> It is unspecified whether the return values of two successive calls to <span class="RenoLink">current_exception</span> refer to the same exception object.</li>
|
||||||
<li> Correct implementation of <span class="RenoLink">current_exception</span> may require compiler support, unless <span class="RenoLink"><a href="enable_current_exception.html">enable_current_exception</a></span> was used at the time the currently handled exception object was passed to throw. Whenever <span class="RenoLink">current_exception</span> fails to properly copy the current exception object, it returns an <span class="RenoLink"><a href="exception_ptr.html">exception_ptr</a></span> to an object of type that is as close as possible to the original exception type, using <span class="RenoLink"><a href="unknown_exception.html">unknown_exception</a></span> as a final fallback. All such types derive from boost::<span class="RenoLink"><a href="exception.html">exception</a></span>, and:<div><ul><li> if the original exception object derives from boost::<span class="RenoLink"><a href="exception.html">exception</a></span>, then the boost::<span class="RenoLink"><a href="exception.html">exception</a></span> sub-object of the object referred to by the returned <span class="RenoLink"><a href="exception_ptr.html">exception_ptr</a></span> is initialized by the boost::<span class="RenoLink"><a href="exception.html">exception</a></span> copy constructor;</li>
|
<li> Correct implementation of <span class="RenoLink">current_exception</span> may require compiler support (e.g. C++11 <a href="https://en.cppreference.com/w/cpp/error/current_exception">std::current_exception()</a> is used when available, as specified by Boost.Config BOOST_NO_CXX11_HDR_EXCEPTION), unless <span class="RenoLink"><a href="enable_current_exception.html">enable_current_exception</a></span> was used at the time the currently handled exception object was passed to throw. Whenever <span class="RenoLink">current_exception</span> fails to properly copy the current exception object, it returns an <span class="RenoLink"><a href="exception_ptr.html">exception_ptr</a></span> to an object of type that is as close as possible to the original exception type, using <span class="RenoLink"><a href="unknown_exception.html">unknown_exception</a></span> as a final fallback. All such types derive from boost::<span class="RenoLink"><a href="exception.html">exception</a></span>, and:<div><ul><li> if the original exception object derives from boost::<span class="RenoLink"><a href="exception.html">exception</a></span>, then the boost::<span class="RenoLink"><a href="exception.html">exception</a></span> sub-object of the object referred to by the returned <span class="RenoLink"><a href="exception_ptr.html">exception_ptr</a></span> is initialized by the boost::<span class="RenoLink"><a href="exception.html">exception</a></span> copy constructor;</li>
|
||||||
<li> if available, the exception contains the std::type_info of the original exception object, accessible through <span class="RenoLink"><a href="get_error_info.html">get_error_info</a></span><<span class="RenoLink"><a href="original_exception_type.html">original_exception_type</a></span>>.</li>
|
<li> if available, the exception contains the std::type_info of the original exception object, accessible through <span class="RenoLink"><a href="get_error_info.html">get_error_info</a></span><<span class="RenoLink"><a href="original_exception_type.html">original_exception_type</a></span>>.</li>
|
||||||
</ul></div>
|
</ul></div>
|
||||||
</li>
|
</li>
|
||||||
|
@ -412,11 +412,14 @@ boost
|
|||||||
{
|
{
|
||||||
return exception_detail::current_exception_std_exception(e);
|
return exception_detail::current_exception_std_exception(e);
|
||||||
}
|
}
|
||||||
|
#ifdef BOOST_NO_CXX11_HDR_EXCEPTION
|
||||||
|
// this case can be handled losslesly with std::current_exception() (see below)
|
||||||
catch(
|
catch(
|
||||||
std::exception & e )
|
std::exception & e )
|
||||||
{
|
{
|
||||||
return exception_detail::current_exception_unknown_std_exception(e);
|
return exception_detail::current_exception_unknown_std_exception(e);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
catch(
|
catch(
|
||||||
boost::exception & e )
|
boost::exception & e )
|
||||||
{
|
{
|
||||||
@ -425,7 +428,22 @@ boost
|
|||||||
catch(
|
catch(
|
||||||
... )
|
... )
|
||||||
{
|
{
|
||||||
|
#ifndef BOOST_NO_CXX11_HDR_EXCEPTION
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// wrap the std::exception_ptr in a clone-enabled Boost.Exception object
|
||||||
|
exception_detail::clone_base const& base =
|
||||||
|
boost::enable_current_exception(std::current_exception());
|
||||||
|
return exception_ptr(shared_ptr<exception_detail::clone_base const>(base.clone()));
|
||||||
|
}
|
||||||
|
catch(
|
||||||
|
...)
|
||||||
|
{
|
||||||
|
return exception_detail::current_exception_unknown_exception();
|
||||||
|
}
|
||||||
|
#else
|
||||||
return exception_detail::current_exception_unknown_exception();
|
return exception_detail::current_exception_unknown_exception();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -461,7 +479,20 @@ boost
|
|||||||
rethrow_exception( exception_ptr const & p )
|
rethrow_exception( exception_ptr const & p )
|
||||||
{
|
{
|
||||||
BOOST_ASSERT(p);
|
BOOST_ASSERT(p);
|
||||||
|
#ifndef BOOST_NO_CXX11_HDR_EXCEPTION
|
||||||
|
try
|
||||||
|
{
|
||||||
|
p.ptr_->rethrow();
|
||||||
|
}
|
||||||
|
catch(
|
||||||
|
const std::exception_ptr& std_ep)
|
||||||
|
{
|
||||||
|
// if an std::exception_ptr was wrapped above then rethrow it
|
||||||
|
std::rethrow_exception(std_ep);
|
||||||
|
}
|
||||||
|
#else
|
||||||
p.ptr_->rethrow();
|
p.ptr_->rethrow();
|
||||||
|
#endif
|
||||||
BOOST_ASSERT(0);
|
BOOST_ASSERT(0);
|
||||||
#if defined(UNDER_CE)
|
#if defined(UNDER_CE)
|
||||||
// some CE platforms don't define ::abort()
|
// some CE platforms don't define ::abort()
|
||||||
|
@ -38,6 +38,25 @@ throw_unknown_exception()
|
|||||||
throw test_exception();
|
throw test_exception();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct
|
||||||
|
user_defined_exception
|
||||||
|
{
|
||||||
|
user_defined_exception(int d):data(d){}
|
||||||
|
int data;
|
||||||
|
};
|
||||||
|
|
||||||
|
void
|
||||||
|
throw_user_defined_exception()
|
||||||
|
{
|
||||||
|
throw user_defined_exception(42);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
throw_builtin_exception()
|
||||||
|
{
|
||||||
|
throw 42;
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main()
|
main()
|
||||||
{
|
{
|
||||||
@ -138,5 +157,115 @@ main()
|
|||||||
BOOST_TEST(false);
|
BOOST_TEST(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
throw_user_defined_exception();
|
||||||
|
}
|
||||||
|
catch(
|
||||||
|
... )
|
||||||
|
{
|
||||||
|
boost::exception_ptr ep=boost::current_exception();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
rethrow_exception(ep);
|
||||||
|
}
|
||||||
|
#ifndef BOOST_NO_CXX11_HDR_EXCEPTION
|
||||||
|
catch(
|
||||||
|
user_defined_exception & x)
|
||||||
|
{
|
||||||
|
//Yay! std::current_exception to the rescue!
|
||||||
|
BOOST_TEST( 42==x.data );
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
catch(
|
||||||
|
boost::unknown_exception & )
|
||||||
|
{
|
||||||
|
//Boo! user defined exception was transported as a boost::unknown_exception
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
catch(
|
||||||
|
... )
|
||||||
|
{
|
||||||
|
BOOST_TEST(false);
|
||||||
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
rethrow_exception(ep);
|
||||||
|
}
|
||||||
|
#ifndef BOOST_NO_CXX11_HDR_EXCEPTION
|
||||||
|
catch(
|
||||||
|
user_defined_exception & x)
|
||||||
|
{
|
||||||
|
//Yay! std::current_exception to the rescue!
|
||||||
|
BOOST_TEST( 42==x.data );
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
catch(
|
||||||
|
boost::unknown_exception & )
|
||||||
|
{
|
||||||
|
//Boo! user defined exception was transported as a boost::unknown_exception
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
catch(
|
||||||
|
... )
|
||||||
|
{
|
||||||
|
BOOST_TEST(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
throw_builtin_exception();
|
||||||
|
}
|
||||||
|
catch(
|
||||||
|
... )
|
||||||
|
{
|
||||||
|
boost::exception_ptr ep=boost::current_exception();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
rethrow_exception(ep);
|
||||||
|
}
|
||||||
|
#ifndef BOOST_NO_CXX11_HDR_EXCEPTION
|
||||||
|
catch(
|
||||||
|
int & x)
|
||||||
|
{
|
||||||
|
//Yay! std::current_exception to the rescue!
|
||||||
|
BOOST_TEST( 42==x );
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
catch(
|
||||||
|
boost::unknown_exception & )
|
||||||
|
{
|
||||||
|
//Boo! builtin exception was transported as a boost::unknown_exception
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
catch(
|
||||||
|
... )
|
||||||
|
{
|
||||||
|
BOOST_TEST(false);
|
||||||
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
rethrow_exception(ep);
|
||||||
|
}
|
||||||
|
#ifndef BOOST_NO_CXX11_HDR_EXCEPTION
|
||||||
|
catch(
|
||||||
|
int & x)
|
||||||
|
{
|
||||||
|
//Yay! std::current_exception to the rescue!
|
||||||
|
BOOST_TEST( 42==x );
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
catch(
|
||||||
|
boost::unknown_exception & )
|
||||||
|
{
|
||||||
|
//Boo! builtin exception was transported as a boost::unknown_exception
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
catch(
|
||||||
|
... )
|
||||||
|
{
|
||||||
|
BOOST_TEST(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
return boost::report_errors();
|
return boost::report_errors();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user