mirror of
https://github.com/boostorg/exception.git
synced 2025-07-29 12:07:20 +02:00
N2179 compliance (pending documentation update)
[SVN r44438]
This commit is contained in:
@ -5,7 +5,7 @@
|
||||
|
||||
//This example shows how to enable cloning when throwing a boost::exception.
|
||||
|
||||
#include <boost/exception/enable_exception_cloning.hpp>
|
||||
#include <boost/exception/enable_current_exception.hpp>
|
||||
#include <boost/exception/info.hpp>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
@ -18,5 +18,6 @@ void
|
||||
file_read( FILE * f, void * buffer, size_t size )
|
||||
{
|
||||
if( size!=fread(buffer,1,size,f) )
|
||||
throw boost::enable_exception_cloning(file_read_error()) << errno_info(errno);
|
||||
throw boost::enable_current_exception(file_read_error()) <<
|
||||
errno_info(errno);
|
||||
}
|
||||
|
@ -20,13 +20,13 @@ worker_thread( boost::exception_ptr & error )
|
||||
error = boost::exception_ptr();
|
||||
}
|
||||
catch(
|
||||
boost::exception & e )
|
||||
... )
|
||||
{
|
||||
error = boost::clone_exception(e);
|
||||
error = boost::current_exception();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// ...continued
|
||||
|
||||
void
|
||||
work()
|
||||
|
@ -6,10 +6,10 @@
|
||||
#ifndef UUID_FA5836A2CADA11DC8CD47C8555D89593
|
||||
#define UUID_FA5836A2CADA11DC8CD47C8555D89593
|
||||
|
||||
#include <boost/exception/enable_exception_cloning.hpp>
|
||||
#include <boost/exception/enable_current_exception.hpp>
|
||||
#include <boost/exception/exception.hpp>
|
||||
#include <boost/exception/detail/cloning_base.hpp>
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace
|
||||
boost
|
||||
@ -21,31 +21,165 @@ boost
|
||||
{
|
||||
public:
|
||||
|
||||
explicit
|
||||
unknown_exception()
|
||||
{
|
||||
}
|
||||
|
||||
explicit
|
||||
unknown_exception( boost::exception const & x ):
|
||||
boost::exception(x)
|
||||
unknown_exception( boost::exception const & e ):
|
||||
boost::exception(e)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
typedef intrusive_ptr<exception_detail::clone_base const> exception_ptr;
|
||||
|
||||
namespace
|
||||
exception_detail
|
||||
{
|
||||
template <class T>
|
||||
class
|
||||
current_exception_std_exception_wrapper:
|
||||
public T,
|
||||
public boost::exception
|
||||
{
|
||||
public:
|
||||
|
||||
explicit
|
||||
current_exception_std_exception_wrapper( T const & e1 ):
|
||||
T(e1)
|
||||
{
|
||||
}
|
||||
|
||||
current_exception_std_exception_wrapper( T const & e1, boost::exception const & e2 ):
|
||||
T(e1),
|
||||
boost::exception(e2)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
exception_ptr
|
||||
clone_exception( T const & e )
|
||||
current_exception_std_exception( T const & e1 )
|
||||
{
|
||||
if( boost::exception const * e2 = dynamic_cast<boost::exception const *>(&e1) )
|
||||
return exception_ptr(exception_detail::make_clone(current_exception_std_exception_wrapper<T>(e1,*e2)));
|
||||
else
|
||||
return exception_ptr(exception_detail::make_clone(current_exception_std_exception_wrapper<T>(e1)));
|
||||
}
|
||||
|
||||
inline
|
||||
exception_ptr
|
||||
current_exception_unknown_exception()
|
||||
{
|
||||
return exception_ptr(exception_detail::make_clone(unknown_exception()));
|
||||
}
|
||||
|
||||
inline
|
||||
exception_ptr
|
||||
current_exception_unknown_std_exception( std::exception const & e )
|
||||
{
|
||||
if( boost::exception_detail::cloning_base const * cb = dynamic_cast<boost::exception_detail::cloning_base const *>(&e) )
|
||||
if( exception_detail::clone_base const * c = cb->clone() )
|
||||
return exception_ptr(c);
|
||||
if( boost::exception const * be = dynamic_cast<boost::exception const *>(&e) )
|
||||
return exception_ptr(exception_detail::make_clone(unknown_exception(*be)));
|
||||
else
|
||||
return exception_ptr(exception_detail::make_clone(unknown_exception()));
|
||||
return current_exception_unknown_exception();
|
||||
}
|
||||
|
||||
inline
|
||||
exception_ptr
|
||||
current_exception_unknown_boost_exception( boost::exception const & e )
|
||||
{
|
||||
return exception_ptr(exception_detail::make_clone(unknown_exception(e)));
|
||||
}
|
||||
}
|
||||
|
||||
inline
|
||||
exception_ptr
|
||||
current_exception()
|
||||
{
|
||||
try
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch(
|
||||
exception_detail::cloning_base & e )
|
||||
{
|
||||
exception_detail::clone_base const * c = e.clone();
|
||||
BOOST_ASSERT(c!=0);
|
||||
return exception_ptr(c);
|
||||
}
|
||||
catch(
|
||||
... )
|
||||
{
|
||||
}
|
||||
try
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch(
|
||||
std::invalid_argument & e )
|
||||
{
|
||||
return exception_detail::current_exception_std_exception(e);
|
||||
}
|
||||
catch(
|
||||
std::out_of_range & e )
|
||||
{
|
||||
return exception_detail::current_exception_std_exception(e);
|
||||
}
|
||||
catch(
|
||||
std::logic_error & e )
|
||||
{
|
||||
return exception_detail::current_exception_std_exception(e);
|
||||
}
|
||||
catch(
|
||||
std::bad_alloc & e )
|
||||
{
|
||||
return exception_detail::current_exception_std_exception(e);
|
||||
}
|
||||
catch(
|
||||
std::bad_cast & e )
|
||||
{
|
||||
return exception_detail::current_exception_std_exception(e);
|
||||
}
|
||||
catch(
|
||||
std::bad_typeid & e )
|
||||
{
|
||||
return exception_detail::current_exception_std_exception(e);
|
||||
}
|
||||
catch(
|
||||
std::bad_exception & e )
|
||||
{
|
||||
return exception_detail::current_exception_std_exception(e);
|
||||
}
|
||||
catch(
|
||||
std::exception & e )
|
||||
{
|
||||
return exception_detail::current_exception_unknown_std_exception(e);
|
||||
}
|
||||
catch(
|
||||
boost::exception & e )
|
||||
{
|
||||
return exception_detail::current_exception_unknown_boost_exception(e);
|
||||
}
|
||||
catch(
|
||||
... )
|
||||
{
|
||||
return exception_detail::current_exception_unknown_exception();
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
exception_ptr
|
||||
copy_exception( T const & e )
|
||||
{
|
||||
try
|
||||
{
|
||||
throw enable_current_exception(e);
|
||||
}
|
||||
catch( ... )
|
||||
{
|
||||
return current_exception();
|
||||
}
|
||||
}
|
||||
|
||||
inline
|
||||
|
@ -139,7 +139,7 @@ boost
|
||||
|
||||
template <class T>
|
||||
exception_detail::clone_impl<T>
|
||||
enable_exception_cloning( T const & x )
|
||||
enable_current_exception( T const & x )
|
||||
{
|
||||
return exception_detail::clone_impl<T>(x);
|
||||
}
|
@ -16,6 +16,7 @@ compile-fail to_string_fail.cpp ;
|
||||
|
||||
#exception
|
||||
run cloning_test.cpp ;
|
||||
run copy_exception_test.cpp ;
|
||||
run unknown_exception_test.cpp ;
|
||||
run exception_test.cpp ;
|
||||
run boost_error_info_test.cpp ;
|
||||
|
@ -17,12 +17,12 @@ main()
|
||||
{
|
||||
try
|
||||
{
|
||||
throw boost::enable_exception_cloning(test_exception());
|
||||
throw boost::enable_current_exception(test_exception());
|
||||
}
|
||||
catch(
|
||||
std::exception & x )
|
||||
... )
|
||||
{
|
||||
boost::exception_ptr p = boost::clone_exception(x);
|
||||
boost::exception_ptr p = boost::current_exception();
|
||||
try
|
||||
{
|
||||
rethrow_exception(p);
|
||||
|
34
test/copy_exception_test.cpp
Normal file
34
test/copy_exception_test.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
|
||||
|
||||
//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)
|
||||
|
||||
#include <boost/exception/cloning.hpp>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
|
||||
struct
|
||||
test_exception:
|
||||
std::exception
|
||||
{
|
||||
};
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
boost::exception_ptr p = boost::copy_exception(test_exception());
|
||||
try
|
||||
{
|
||||
rethrow_exception(p);
|
||||
BOOST_TEST(false);
|
||||
}
|
||||
catch(
|
||||
test_exception & )
|
||||
{
|
||||
}
|
||||
catch(
|
||||
... )
|
||||
{
|
||||
BOOST_TEST(false);
|
||||
}
|
||||
return boost::report_errors();
|
||||
}
|
@ -35,9 +35,9 @@ tester()
|
||||
BOOST_ASSERT(false);
|
||||
}
|
||||
catch(
|
||||
std::exception & x )
|
||||
... )
|
||||
{
|
||||
boost::exception_ptr p = boost::clone_exception(x);
|
||||
boost::exception_ptr p = boost::current_exception();
|
||||
try
|
||||
{
|
||||
rethrow_exception(p);
|
||||
@ -55,11 +55,6 @@ tester()
|
||||
BOOST_TEST(false);
|
||||
}
|
||||
}
|
||||
catch(
|
||||
... )
|
||||
{
|
||||
BOOST_TEST(false);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -40,9 +40,9 @@ main()
|
||||
throw_boost_exception();
|
||||
}
|
||||
catch(
|
||||
boost::exception & x )
|
||||
... )
|
||||
{
|
||||
boost::exception_ptr ep=boost::clone_exception(x);
|
||||
boost::exception_ptr ep=boost::current_exception();
|
||||
try
|
||||
{
|
||||
rethrow_exception(ep);
|
||||
@ -53,6 +53,20 @@ main()
|
||||
BOOST_TEST( 42==*boost::get_error_info<test>(x) );
|
||||
}
|
||||
catch(
|
||||
... )
|
||||
{
|
||||
BOOST_TEST(false);
|
||||
}
|
||||
try
|
||||
{
|
||||
rethrow_exception(ep);
|
||||
}
|
||||
catch(
|
||||
boost::exception & x )
|
||||
{
|
||||
BOOST_TEST( 42==*boost::get_error_info<test>(x) );
|
||||
}
|
||||
catch(
|
||||
... )
|
||||
{
|
||||
BOOST_TEST(false);
|
||||
@ -63,9 +77,9 @@ main()
|
||||
throw_unknown_exception();
|
||||
}
|
||||
catch(
|
||||
std::exception & x )
|
||||
... )
|
||||
{
|
||||
boost::exception_ptr ep=boost::clone_exception(x);
|
||||
boost::exception_ptr ep=boost::current_exception();
|
||||
try
|
||||
{
|
||||
rethrow_exception(ep);
|
||||
@ -75,6 +89,19 @@ main()
|
||||
{
|
||||
}
|
||||
catch(
|
||||
... )
|
||||
{
|
||||
BOOST_TEST(false);
|
||||
}
|
||||
try
|
||||
{
|
||||
rethrow_exception(ep);
|
||||
}
|
||||
catch(
|
||||
boost::exception & )
|
||||
{
|
||||
}
|
||||
catch(
|
||||
... )
|
||||
{
|
||||
BOOST_TEST(false);
|
||||
|
Reference in New Issue
Block a user