sl@0: // boost/catch_exceptions.hpp -----------------------------------------------// sl@0: sl@0: // Copyright Beman Dawes 1995-2001. Distributed under the Boost sl@0: // Software License, Version 1.0. (See accompanying file sl@0: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) sl@0: sl@0: // See http://www.boost.org/libs/test for documentation. sl@0: sl@0: // Revision History sl@0: // 13 Jun 01 report_exception() made inline. (John Maddock, Jesse Jones) sl@0: // 26 Feb 01 Numerous changes suggested during formal review. (Beman) sl@0: // 25 Jan 01 catch_exceptions.hpp code factored out of cpp_main.cpp. sl@0: // 22 Jan 01 Remove test_tools dependencies to reduce coupling. sl@0: // 5 Nov 00 Initial boost version (Beman Dawes) sl@0: sl@0: #ifndef BOOST_CATCH_EXCEPTIONS_HPP sl@0: #define BOOST_CATCH_EXCEPTIONS_HPP sl@0: sl@0: // header dependencies are deliberately restricted to the standard library sl@0: // to reduce coupling to other boost libraries. sl@0: #include // for string sl@0: #include // for bad_alloc sl@0: #include // for bad_cast, bad_typeid sl@0: #include // for exception, bad_exception sl@0: #include // for std exception hierarchy sl@0: #include // for exit codes sl@0: # if __GNUC__ != 2 || __GNUC_MINOR__ > 96 sl@0: # include // for ostream sl@0: # else sl@0: # include // workaround GNU missing ostream header sl@0: # endif sl@0: sl@0: # if defined(__BORLANDC__) && (__BORLANDC__ <= 0x0551) sl@0: # define BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT sl@0: # endif sl@0: sl@0: #if defined(MPW_CPLUS) && (MPW_CPLUS <= 0x890) sl@0: # define BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT sl@0: namespace std { class bad_typeid { }; } sl@0: # endif sl@0: sl@0: namespace boost sl@0: { sl@0: sl@0: namespace detail sl@0: { sl@0: // A separate reporting function was requested during formal review. sl@0: inline void report_exception( std::ostream & os, sl@0: const char * name, const char * info ) sl@0: { os << "\n** uncaught exception: " << name << " " << info << std::endl; } sl@0: } sl@0: sl@0: // catch_exceptions ------------------------------------------------------// sl@0: sl@0: template< class Generator > // Generator is function object returning int sl@0: int catch_exceptions( Generator function_object, sl@0: std::ostream & out, std::ostream & err ) sl@0: { sl@0: int result = 0; // quiet compiler warnings sl@0: bool exception_thrown = true; // avoid setting result for each excptn type sl@0: sl@0: #ifndef BOOST_NO_EXCEPTIONS sl@0: try sl@0: { sl@0: #endif sl@0: result = function_object(); sl@0: exception_thrown = false; sl@0: #ifndef BOOST_NO_EXCEPTIONS sl@0: } sl@0: sl@0: // As a result of hard experience with strangely interleaved output sl@0: // under some compilers, there is a lot of use of endl in the code below sl@0: // where a simple '\n' might appear to do. sl@0: sl@0: // The rules for catch & arguments are a bit different from function sl@0: // arguments (ISO 15.3 paragraphs 18 & 19). Apparently const isn't sl@0: // required, but it doesn't hurt and some programmers ask for it. sl@0: sl@0: catch ( const char * ex ) sl@0: { detail::report_exception( out, "", ex ); } sl@0: catch ( const std::string & ex ) sl@0: { detail::report_exception( out, "", ex.c_str() ); } sl@0: sl@0: // std:: exceptions sl@0: catch ( const std::bad_alloc & ex ) sl@0: { detail::report_exception( out, "std::bad_alloc:", ex.what() ); } sl@0: sl@0: # ifndef BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT sl@0: catch ( const std::bad_cast & ex ) sl@0: { detail::report_exception( out, "std::bad_cast:", ex.what() ); } sl@0: catch ( const std::bad_typeid & ex ) sl@0: { detail::report_exception( out, "std::bad_typeid:", ex.what() ); } sl@0: # else sl@0: catch ( const std::bad_cast & ) sl@0: { detail::report_exception( out, "std::bad_cast", "" ); } sl@0: catch ( const std::bad_typeid & ) sl@0: { detail::report_exception( out, "std::bad_typeid", "" ); } sl@0: # endif sl@0: sl@0: catch ( const std::bad_exception & ex ) sl@0: { detail::report_exception( out, "std::bad_exception:", ex.what() ); } sl@0: catch ( const std::domain_error & ex ) sl@0: { detail::report_exception( out, "std::domain_error:", ex.what() ); } sl@0: catch ( const std::invalid_argument & ex ) sl@0: { detail::report_exception( out, "std::invalid_argument:", ex.what() ); } sl@0: catch ( const std::length_error & ex ) sl@0: { detail::report_exception( out, "std::length_error:", ex.what() ); } sl@0: catch ( const std::out_of_range & ex ) sl@0: { detail::report_exception( out, "std::out_of_range:", ex.what() ); } sl@0: catch ( const std::range_error & ex ) sl@0: { detail::report_exception( out, "std::range_error:", ex.what() ); } sl@0: catch ( const std::overflow_error & ex ) sl@0: { detail::report_exception( out, "std::overflow_error:", ex.what() ); } sl@0: catch ( const std::underflow_error & ex ) sl@0: { detail::report_exception( out, "std::underflow_error:", ex.what() ); } sl@0: catch ( const std::logic_error & ex ) sl@0: { detail::report_exception( out, "std::logic_error:", ex.what() ); } sl@0: catch ( const std::runtime_error & ex ) sl@0: { detail::report_exception( out, "std::runtime_error:", ex.what() ); } sl@0: catch ( const std::exception & ex ) sl@0: { detail::report_exception( out, "std::exception:", ex.what() ); } sl@0: sl@0: catch ( ... ) sl@0: { detail::report_exception( out, "unknown exception", "" ); } sl@0: #endif // BOOST_NO_EXCEPTIONS sl@0: sl@0: if ( exception_thrown ) result = boost::exit_exception_failure; sl@0: sl@0: if ( result != 0 && result != exit_success ) sl@0: { sl@0: out << std::endl << "**** returning with error code " sl@0: << result << std::endl; sl@0: err sl@0: << "********** errors detected; see stdout for details ***********" sl@0: << std::endl; sl@0: } sl@0: #if !defined(BOOST_NO_CPP_MAIN_SUCCESS_MESSAGE) sl@0: else { out << std::flush << "no errors detected" << std::endl; } sl@0: #endif sl@0: return result; sl@0: } // catch_exceptions sl@0: sl@0: } // boost sl@0: sl@0: #endif // BOOST_CATCH_EXCEPTIONS_HPP sl@0: