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