Files
boost_exception/test/unknown_exception_test.cpp

278 lines
5.8 KiB
C++
Raw Permalink Normal View History

2009-04-07 03:02:15 +00:00
//Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
2008-03-04 01:41:17 +00:00
//Distributed under the Boost Software License, Version 1.0. (See accompanying
//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
2020-12-22 17:26:53 -08:00
#include <boost/config.hpp>
#if defined( BOOST_NO_EXCEPTIONS )
# error This program requires exception handling.
#endif
#include <boost/exception_ptr.hpp>
#include <boost/exception/get_error_info.hpp>
2008-03-04 01:41:17 +00:00
#include <boost/exception/info.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <boost/detail/workaround.hpp>
#if BOOST_WORKAROUND(BOOST_CODEGEARC, BOOST_TESTED_AT(0x610))
struct tag_test {};
#endif
2008-03-04 01:41:17 +00:00
typedef boost::error_info<struct tag_test,int> test;
struct
test_boost_exception:
boost::exception
{
};
2008-03-04 01:41:17 +00:00
void
throw_boost_exception()
{
throw test_boost_exception() << test(42);
}
2008-03-04 01:41:17 +00:00
void
throw_unknown_exception()
{
struct
test_exception:
std::exception
{
};
throw test_exception();
}
2008-03-04 01:41:17 +00:00
Attempt to solve issue #10 (#24) * 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).
2020-01-29 22:41:48 +01:00
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;
}
2008-03-04 01:41:17 +00:00
int
main()
{
try
{
throw_boost_exception();
}
catch(
... )
{
boost::exception_ptr ep=boost::current_exception();
try
{
rethrow_exception(ep);
}
catch(
boost::unknown_exception & x )
{
if( int const * d=boost::get_error_info<test>(x) )
BOOST_TEST( 42==*d );
else
BOOST_TEST(false);
}
catch(
boost::exception & x )
{
//Yay! Non-intrusive cloning supported!
if( int const * d=boost::get_error_info<test>(x) )
BOOST_TEST( 42==*d );
else
BOOST_TEST(false);
}
catch(
... )
{
BOOST_TEST(false);
}
try
{
rethrow_exception(ep);
}
catch(
boost::exception & x )
{
if( int const * d=boost::get_error_info<test>(x) )
BOOST_TEST( 42==*d );
else
BOOST_TEST(false);
}
catch(
... )
{
BOOST_TEST(false);
}
}
try
{
throw_unknown_exception();
}
catch(
... )
{
boost::exception_ptr ep=boost::current_exception();
try
{
rethrow_exception(ep);
}
catch(
boost::unknown_exception & )
{
}
catch(
std::exception & )
{
//Yay! Non-intrusive cloning supported!
}
catch(
... )
{
BOOST_TEST(false);
}
try
{
rethrow_exception(ep);
}
catch(
boost::exception & )
{
}
catch(
std::exception & )
{
//Yay! Non-intrusive cloning supported!
}
catch(
... )
{
BOOST_TEST(false);
}
}
Attempt to solve issue #10 (#24) * 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).
2020-01-29 22:41:48 +01:00
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();
}