1// Copyright 2018, 2019 Peter Dimov
2//
3// Distributed under the Boost Software License, Version 1.0.
4//
5// See accompanying file LICENSE_1_0.txt or copy at
6// http://www.boost.org/LICENSE_1_0.txt
7
8#if defined(__clang__)
9# pragma clang diagnostic ignored "-Wunknown-pragmas"
10# pragma clang diagnostic ignored "-Wunknown-warning-option"
11# pragma clang diagnostic ignored "-Wpotentially-evaluated-expression"
12# pragma clang diagnostic ignored "-Wdelete-non-abstract-non-virtual-dtor"
13#endif
14
15#include <boost/throw_exception.hpp>
16#include <boost/exception/get_error_info.hpp>
17#include <boost/exception/info.hpp>
18#include <boost/detail/lightweight_test.hpp>
19#include <string>
20
21typedef boost::error_info<struct tag_error_code, int> error_code;
22typedef boost::error_info<struct tag_error_string, std::string> error_string;
23
24class my_exception: public std::exception, public boost::exception
25{
26};
27
28class my_exception2: public std::exception, public virtual boost::exception
29{
30};
31
32int main()
33{
34 try
35 {
36 boost::throw_exception( e: my_exception() << error_code( 123 ) << error_string( "error%%string" ) );
37 }
38 catch( boost::exception const & x )
39 {
40 {
41 int const * code = boost::get_error_info<error_code>( some_exception: x );
42
43 BOOST_TEST( code != 0 );
44 BOOST_TEST_EQ( *code, 123 );
45 }
46
47 {
48 std::string const * str = boost::get_error_info<error_string>( some_exception: x );
49
50 BOOST_TEST( str != 0 );
51 BOOST_TEST_EQ( *str, "error%%string" );
52 }
53 }
54
55 try
56 {
57 BOOST_THROW_EXCEPTION( my_exception() << error_code( 123 ) << error_string( "error%%string" ) );
58 }
59 catch( boost::exception const & x )
60 {
61 {
62 int const * code = boost::get_error_info<error_code>( some_exception: x );
63
64 BOOST_TEST( code != 0 );
65 BOOST_TEST_EQ( *code, 123 );
66 }
67
68 {
69 std::string const * str = boost::get_error_info<error_string>( some_exception: x );
70
71 BOOST_TEST( str != 0 );
72 BOOST_TEST_EQ( *str, "error%%string" );
73 }
74 }
75
76 try
77 {
78 boost::throw_exception( e: my_exception2() << error_code( 123 ) << error_string( "error%%string" ) );
79 }
80 catch( boost::exception const & x )
81 {
82 {
83 int const * code = boost::get_error_info<error_code>( some_exception: x );
84
85 BOOST_TEST( code != 0 );
86 BOOST_TEST_EQ( *code, 123 );
87 }
88
89 {
90 std::string const * str = boost::get_error_info<error_string>( some_exception: x );
91
92 BOOST_TEST( str != 0 );
93 BOOST_TEST_EQ( *str, "error%%string" );
94 }
95 }
96
97 try
98 {
99 BOOST_THROW_EXCEPTION( my_exception2() << error_code( 123 ) << error_string( "error%%string" ) );
100 }
101 catch( boost::exception const & x )
102 {
103 {
104 int const * code = boost::get_error_info<error_code>( some_exception: x );
105
106 BOOST_TEST( code != 0 );
107 BOOST_TEST_EQ( *code, 123 );
108 }
109
110 {
111 std::string const * str = boost::get_error_info<error_string>( some_exception: x );
112
113 BOOST_TEST( str != 0 );
114 BOOST_TEST_EQ( *str, "error%%string" );
115 }
116 }
117
118 return boost::report_errors();
119}
120

source code of boost/libs/throw_exception/test/throw_exception_test5.cpp