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