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