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