1 //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc. 2 3 //Distributed under the Boost Software License, Version 1.0. (See accompanying 4 //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5 6 #include "helper2.hpp" 7 #include <boost/exception/get_error_info.hpp> 8 #include <boost/exception/info.hpp> 9 #include <boost/exception_ptr.hpp> 10 #include <boost/detail/lightweight_test.hpp> 11 12 typedef boost::error_info<struct tag_test_int,int> test_data; 13 14 struct 15 exception1: 16 std::exception 17 { 18 }; 19 20 struct 21 exception2: 22 std::exception, 23 boost::exception 24 { 25 }; 26 27 void boost_throw_exception_test()28boost_throw_exception_test() 29 { 30 try 31 { 32 BOOST_THROW_EXCEPTION(exception1()); 33 BOOST_ERROR("BOOST_THROW_EXCEPTION failed to throw."); 34 } 35 catch( 36 boost::exception & x ) 37 { 38 char const * const * function=boost::get_error_info<boost::throw_function>(x); 39 char const * const * file=boost::get_error_info<boost::throw_file>(x); 40 int const * line=boost::get_error_info<boost::throw_line>(x); 41 BOOST_TEST( file && *file ); 42 BOOST_TEST( function && *function ); 43 BOOST_TEST( line && *line==32 ); 44 } 45 catch( 46 ... ) 47 { 48 BOOST_TEST(false); 49 } 50 try 51 { 52 BOOST_THROW_EXCEPTION(exception2() << test_data(42)); 53 BOOST_ERROR("BOOST_THROW_EXCEPTION failed to throw."); 54 } 55 catch( 56 boost::exception & x ) 57 { 58 char const * const * function=boost::get_error_info<boost::throw_function>(x); 59 char const * const * file=boost::get_error_info<boost::throw_file>(x); 60 int const * line=boost::get_error_info<boost::throw_line>(x); 61 int const * data=boost::get_error_info<test_data>(x); 62 BOOST_TEST( file && *file ); 63 BOOST_TEST( function && *function ); 64 BOOST_TEST( line && *line==52 ); 65 BOOST_TEST( data && *data==42 ); 66 } 67 catch( 68 ... ) 69 { 70 BOOST_TEST(false); 71 } 72 } 73 74 void throw_fwd(void (* thrower)(int))75throw_fwd( void (*thrower)(int) ) 76 { 77 try 78 { 79 thrower(42); 80 } 81 catch( 82 boost::exception & x ) 83 { 84 x << test_data(42); 85 throw; 86 } 87 } 88 89 template <class T> 90 void tester()91tester() 92 { 93 try 94 { 95 throw_fwd( &boost::exception_test::throw_test_exception<T> ); 96 BOOST_ASSERT(false); 97 } 98 catch( 99 ... ) 100 { 101 boost::exception_ptr p = boost::current_exception(); 102 try 103 { 104 rethrow_exception(p); 105 BOOST_TEST(false); 106 } 107 catch( 108 T & y ) 109 { 110 #ifdef BOOST_NO_RTTI 111 try 112 { 113 throw; 114 } 115 catch( 116 boost::exception & y ) 117 { 118 #endif 119 BOOST_TEST(boost::get_error_info<test_data>(y)); 120 if( int const * d=boost::get_error_info<test_data>(y) ) 121 BOOST_TEST(*d==42); 122 #ifdef BOOST_NO_RTTI 123 } 124 catch( 125 ... ) 126 { 127 BOOST_TEST(false); 128 } 129 #endif 130 BOOST_TEST(y.x_==42); 131 } 132 catch( 133 ... ) 134 { 135 BOOST_TEST(false); 136 } 137 } 138 } 139 140 int main()141main() 142 { 143 boost_throw_exception_test(); 144 tester<boost::exception_test::derives_boost_exception>(); 145 tester<boost::exception_test::derives_boost_exception_virtually>(); 146 tester<boost::exception_test::derives_std_exception>(); 147 return boost::report_errors(); 148 } 149