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