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 <boost/exception_ptr.hpp> 13 #include <boost/exception/get_error_info.hpp> 14 #include <boost/exception/info.hpp> 15 #include <boost/detail/lightweight_test.hpp> 16 #include <boost/detail/workaround.hpp> 17 18 #if BOOST_WORKAROUND(BOOST_CODEGEARC, BOOST_TESTED_AT(0x610)) 19 struct tag_test {}; 20 #endif 21 22 typedef boost::error_info<struct tag_test,int> test; 23 24 struct 25 test_boost_exception: 26 boost::exception 27 { 28 }; 29 30 void throw_boost_exception()31throw_boost_exception() 32 { 33 throw test_boost_exception() << test(42); 34 } 35 36 void throw_unknown_exception()37throw_unknown_exception() 38 { 39 struct 40 test_exception: 41 std::exception 42 { 43 }; 44 throw test_exception(); 45 } 46 47 struct 48 user_defined_exception 49 { user_defined_exceptionuser_defined_exception50 user_defined_exception(int d):data(d){} 51 int data; 52 }; 53 54 void throw_user_defined_exception()55throw_user_defined_exception() 56 { 57 throw user_defined_exception(42); 58 } 59 60 void throw_builtin_exception()61throw_builtin_exception() 62 { 63 throw 42; 64 } 65 66 int main()67main() 68 { 69 try 70 { 71 throw_boost_exception(); 72 } 73 catch( 74 ... ) 75 { 76 boost::exception_ptr ep=boost::current_exception(); 77 try 78 { 79 rethrow_exception(ep); 80 } 81 catch( 82 boost::unknown_exception & x ) 83 { 84 if( int const * d=boost::get_error_info<test>(x) ) 85 BOOST_TEST( 42==*d ); 86 else 87 BOOST_TEST(false); 88 } 89 catch( 90 boost::exception & x ) 91 { 92 //Yay! Non-intrusive cloning supported! 93 if( int const * d=boost::get_error_info<test>(x) ) 94 BOOST_TEST( 42==*d ); 95 else 96 BOOST_TEST(false); 97 } 98 catch( 99 ... ) 100 { 101 BOOST_TEST(false); 102 } 103 try 104 { 105 rethrow_exception(ep); 106 } 107 catch( 108 boost::exception & x ) 109 { 110 if( int const * d=boost::get_error_info<test>(x) ) 111 BOOST_TEST( 42==*d ); 112 else 113 BOOST_TEST(false); 114 } 115 catch( 116 ... ) 117 { 118 BOOST_TEST(false); 119 } 120 } 121 try 122 { 123 throw_unknown_exception(); 124 } 125 catch( 126 ... ) 127 { 128 boost::exception_ptr ep=boost::current_exception(); 129 try 130 { 131 rethrow_exception(ep); 132 } 133 catch( 134 boost::unknown_exception & ) 135 { 136 } 137 catch( 138 std::exception & ) 139 { 140 //Yay! Non-intrusive cloning supported! 141 } 142 catch( 143 ... ) 144 { 145 BOOST_TEST(false); 146 } 147 try 148 { 149 rethrow_exception(ep); 150 } 151 catch( 152 boost::exception & ) 153 { 154 } 155 catch( 156 std::exception & ) 157 { 158 //Yay! Non-intrusive cloning supported! 159 } 160 catch( 161 ... ) 162 { 163 BOOST_TEST(false); 164 } 165 } 166 try 167 { 168 throw_user_defined_exception(); 169 } 170 catch( 171 ... ) 172 { 173 boost::exception_ptr ep=boost::current_exception(); 174 try 175 { 176 rethrow_exception(ep); 177 } 178 #ifndef BOOST_NO_CXX11_HDR_EXCEPTION 179 catch( 180 user_defined_exception & x) 181 { 182 //Yay! std::current_exception to the rescue! 183 BOOST_TEST( 42==x.data ); 184 } 185 #else 186 catch( 187 boost::unknown_exception & ) 188 { 189 //Boo! user defined exception was transported as a boost::unknown_exception 190 } 191 #endif 192 catch( 193 ... ) 194 { 195 BOOST_TEST(false); 196 } 197 try 198 { 199 rethrow_exception(ep); 200 } 201 #ifndef BOOST_NO_CXX11_HDR_EXCEPTION 202 catch( 203 user_defined_exception & x) 204 { 205 //Yay! std::current_exception to the rescue! 206 BOOST_TEST( 42==x.data ); 207 } 208 #else 209 catch( 210 boost::unknown_exception & ) 211 { 212 //Boo! user defined exception was transported as a boost::unknown_exception 213 } 214 #endif 215 catch( 216 ... ) 217 { 218 BOOST_TEST(false); 219 } 220 } 221 try 222 { 223 throw_builtin_exception(); 224 } 225 catch( 226 ... ) 227 { 228 boost::exception_ptr ep=boost::current_exception(); 229 try 230 { 231 rethrow_exception(ep); 232 } 233 #ifndef BOOST_NO_CXX11_HDR_EXCEPTION 234 catch( 235 int & x) 236 { 237 //Yay! std::current_exception to the rescue! 238 BOOST_TEST( 42==x ); 239 } 240 #else 241 catch( 242 boost::unknown_exception & ) 243 { 244 //Boo! builtin exception was transported as a boost::unknown_exception 245 } 246 #endif 247 catch( 248 ... ) 249 { 250 BOOST_TEST(false); 251 } 252 try 253 { 254 rethrow_exception(ep); 255 } 256 #ifndef BOOST_NO_CXX11_HDR_EXCEPTION 257 catch( 258 int & x) 259 { 260 //Yay! std::current_exception to the rescue! 261 BOOST_TEST( 42==x ); 262 } 263 #else 264 catch( 265 boost::unknown_exception & ) 266 { 267 //Boo! builtin exception was transported as a boost::unknown_exception 268 } 269 #endif 270 catch( 271 ... ) 272 { 273 BOOST_TEST(false); 274 } 275 } 276 return boost::report_errors(); 277 } 278