Chris@16
|
1 // boost/catch_exceptions.hpp -----------------------------------------------//
|
Chris@16
|
2
|
Chris@16
|
3 // Copyright Beman Dawes 1995-2001. Distributed under the Boost
|
Chris@16
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6
|
Chris@16
|
7 // See http://www.boost.org/libs/test for documentation.
|
Chris@16
|
8
|
Chris@16
|
9 // Revision History
|
Chris@16
|
10 // 13 Jun 01 report_exception() made inline. (John Maddock, Jesse Jones)
|
Chris@16
|
11 // 26 Feb 01 Numerous changes suggested during formal review. (Beman)
|
Chris@16
|
12 // 25 Jan 01 catch_exceptions.hpp code factored out of cpp_main.cpp.
|
Chris@16
|
13 // 22 Jan 01 Remove test_tools dependencies to reduce coupling.
|
Chris@16
|
14 // 5 Nov 00 Initial boost version (Beman Dawes)
|
Chris@16
|
15
|
Chris@16
|
16 #ifndef BOOST_CATCH_EXCEPTIONS_HPP
|
Chris@16
|
17 #define BOOST_CATCH_EXCEPTIONS_HPP
|
Chris@16
|
18
|
Chris@16
|
19 // header dependencies are deliberately restricted to the standard library
|
Chris@16
|
20 // to reduce coupling to other boost libraries.
|
Chris@16
|
21 #include <string> // for string
|
Chris@16
|
22 #include <new> // for bad_alloc
|
Chris@16
|
23 #include <typeinfo> // for bad_cast, bad_typeid
|
Chris@16
|
24 #include <exception> // for exception, bad_exception
|
Chris@16
|
25 #include <stdexcept> // for std exception hierarchy
|
Chris@16
|
26 #include <boost/cstdlib.hpp> // for exit codes
|
Chris@101
|
27 #include <ostream> // for ostream
|
Chris@16
|
28
|
Chris@16
|
29 # if defined(__BORLANDC__) && (__BORLANDC__ <= 0x0551)
|
Chris@16
|
30 # define BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT
|
Chris@16
|
31 # endif
|
Chris@16
|
32
|
Chris@16
|
33 #if defined(MPW_CPLUS) && (MPW_CPLUS <= 0x890)
|
Chris@16
|
34 # define BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT
|
Chris@16
|
35 namespace std { class bad_typeid { }; }
|
Chris@16
|
36 # endif
|
Chris@16
|
37
|
Chris@16
|
38 namespace boost
|
Chris@16
|
39 {
|
Chris@16
|
40
|
Chris@16
|
41 namespace detail
|
Chris@16
|
42 {
|
Chris@16
|
43 // A separate reporting function was requested during formal review.
|
Chris@16
|
44 inline void report_exception( std::ostream & os,
|
Chris@16
|
45 const char * name, const char * info )
|
Chris@16
|
46 { os << "\n** uncaught exception: " << name << " " << info << std::endl; }
|
Chris@16
|
47 }
|
Chris@16
|
48
|
Chris@16
|
49 // catch_exceptions ------------------------------------------------------//
|
Chris@16
|
50
|
Chris@16
|
51 template< class Generator > // Generator is function object returning int
|
Chris@16
|
52 int catch_exceptions( Generator function_object,
|
Chris@16
|
53 std::ostream & out, std::ostream & err )
|
Chris@16
|
54 {
|
Chris@16
|
55 int result = 0; // quiet compiler warnings
|
Chris@16
|
56 bool exception_thrown = true; // avoid setting result for each excptn type
|
Chris@16
|
57
|
Chris@16
|
58 #ifndef BOOST_NO_EXCEPTIONS
|
Chris@16
|
59 try
|
Chris@16
|
60 {
|
Chris@16
|
61 #endif
|
Chris@16
|
62 result = function_object();
|
Chris@16
|
63 exception_thrown = false;
|
Chris@16
|
64 #ifndef BOOST_NO_EXCEPTIONS
|
Chris@16
|
65 }
|
Chris@16
|
66
|
Chris@16
|
67 // As a result of hard experience with strangely interleaved output
|
Chris@16
|
68 // under some compilers, there is a lot of use of endl in the code below
|
Chris@16
|
69 // where a simple '\n' might appear to do.
|
Chris@16
|
70
|
Chris@16
|
71 // The rules for catch & arguments are a bit different from function
|
Chris@16
|
72 // arguments (ISO 15.3 paragraphs 18 & 19). Apparently const isn't
|
Chris@16
|
73 // required, but it doesn't hurt and some programmers ask for it.
|
Chris@16
|
74
|
Chris@16
|
75 catch ( const char * ex )
|
Chris@16
|
76 { detail::report_exception( out, "", ex ); }
|
Chris@16
|
77 catch ( const std::string & ex )
|
Chris@16
|
78 { detail::report_exception( out, "", ex.c_str() ); }
|
Chris@16
|
79
|
Chris@16
|
80 // std:: exceptions
|
Chris@16
|
81 catch ( const std::bad_alloc & ex )
|
Chris@16
|
82 { detail::report_exception( out, "std::bad_alloc:", ex.what() ); }
|
Chris@16
|
83
|
Chris@16
|
84 # ifndef BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT
|
Chris@16
|
85 catch ( const std::bad_cast & ex )
|
Chris@16
|
86 { detail::report_exception( out, "std::bad_cast:", ex.what() ); }
|
Chris@16
|
87 catch ( const std::bad_typeid & ex )
|
Chris@16
|
88 { detail::report_exception( out, "std::bad_typeid:", ex.what() ); }
|
Chris@16
|
89 # else
|
Chris@16
|
90 catch ( const std::bad_cast & )
|
Chris@16
|
91 { detail::report_exception( out, "std::bad_cast", "" ); }
|
Chris@16
|
92 catch ( const std::bad_typeid & )
|
Chris@16
|
93 { detail::report_exception( out, "std::bad_typeid", "" ); }
|
Chris@16
|
94 # endif
|
Chris@16
|
95
|
Chris@16
|
96 catch ( const std::bad_exception & ex )
|
Chris@16
|
97 { detail::report_exception( out, "std::bad_exception:", ex.what() ); }
|
Chris@16
|
98 catch ( const std::domain_error & ex )
|
Chris@16
|
99 { detail::report_exception( out, "std::domain_error:", ex.what() ); }
|
Chris@16
|
100 catch ( const std::invalid_argument & ex )
|
Chris@16
|
101 { detail::report_exception( out, "std::invalid_argument:", ex.what() ); }
|
Chris@16
|
102 catch ( const std::length_error & ex )
|
Chris@16
|
103 { detail::report_exception( out, "std::length_error:", ex.what() ); }
|
Chris@16
|
104 catch ( const std::out_of_range & ex )
|
Chris@16
|
105 { detail::report_exception( out, "std::out_of_range:", ex.what() ); }
|
Chris@16
|
106 catch ( const std::range_error & ex )
|
Chris@16
|
107 { detail::report_exception( out, "std::range_error:", ex.what() ); }
|
Chris@16
|
108 catch ( const std::overflow_error & ex )
|
Chris@16
|
109 { detail::report_exception( out, "std::overflow_error:", ex.what() ); }
|
Chris@16
|
110 catch ( const std::underflow_error & ex )
|
Chris@16
|
111 { detail::report_exception( out, "std::underflow_error:", ex.what() ); }
|
Chris@16
|
112 catch ( const std::logic_error & ex )
|
Chris@16
|
113 { detail::report_exception( out, "std::logic_error:", ex.what() ); }
|
Chris@16
|
114 catch ( const std::runtime_error & ex )
|
Chris@16
|
115 { detail::report_exception( out, "std::runtime_error:", ex.what() ); }
|
Chris@16
|
116 catch ( const std::exception & ex )
|
Chris@16
|
117 { detail::report_exception( out, "std::exception:", ex.what() ); }
|
Chris@16
|
118
|
Chris@16
|
119 catch ( ... )
|
Chris@16
|
120 { detail::report_exception( out, "unknown exception", "" ); }
|
Chris@16
|
121 #endif // BOOST_NO_EXCEPTIONS
|
Chris@16
|
122
|
Chris@16
|
123 if ( exception_thrown ) result = boost::exit_exception_failure;
|
Chris@16
|
124
|
Chris@16
|
125 if ( result != 0 && result != exit_success )
|
Chris@16
|
126 {
|
Chris@16
|
127 out << std::endl << "**** returning with error code "
|
Chris@16
|
128 << result << std::endl;
|
Chris@16
|
129 err
|
Chris@16
|
130 << "********** errors detected; see stdout for details ***********"
|
Chris@16
|
131 << std::endl;
|
Chris@16
|
132 }
|
Chris@16
|
133 #if !defined(BOOST_NO_CPP_MAIN_SUCCESS_MESSAGE)
|
Chris@16
|
134 else { out << std::flush << "no errors detected" << std::endl; }
|
Chris@16
|
135 #endif
|
Chris@16
|
136 return result;
|
Chris@16
|
137 } // catch_exceptions
|
Chris@16
|
138
|
Chris@16
|
139 } // boost
|
Chris@16
|
140
|
Chris@16
|
141 #endif // BOOST_CATCH_EXCEPTIONS_HPP
|
Chris@16
|
142
|