Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:35:27

0001 //  boost/catch_exceptions.hpp -----------------------------------------------//
0002 
0003 //  Copyright Beman Dawes 1995-2001.  Distributed under the Boost
0004 //  Software License, Version 1.0. (See accompanying file
0005 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 //  See http://www.boost.org/libs/test for documentation.
0008 
0009 //  Revision History
0010 //   13 Jun 01 report_exception() made inline. (John Maddock, Jesse Jones)
0011 //   26 Feb 01 Numerous changes suggested during formal review. (Beman)
0012 //   25 Jan 01 catch_exceptions.hpp code factored out of cpp_main.cpp.
0013 //   22 Jan 01 Remove test_tools dependencies to reduce coupling.
0014 //    5 Nov 00 Initial boost version (Beman Dawes)
0015 
0016 #ifndef BOOST_CATCH_EXCEPTIONS_HPP
0017 #define BOOST_CATCH_EXCEPTIONS_HPP
0018 
0019 //  header dependencies are deliberately restricted to the standard library
0020 //  to reduce coupling to other boost libraries.
0021 #include <string>             // for string
0022 #include <new>                // for bad_alloc
0023 #include <ostream>            // for ostream
0024 #include <typeinfo>           // for bad_cast, bad_typeid
0025 #include <exception>          // for exception, bad_exception
0026 #include <stdexcept>          // for std exception hierarchy
0027 #include <boost/config.hpp>
0028 #include <boost/cstdlib.hpp>  // for exit codes
0029 
0030 # if defined(BOOST_BORLANDC) && (__BORLANDC__ <= 0x0551)
0031 #   define BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT 
0032 # endif
0033 
0034 #if defined(MPW_CPLUS) && (MPW_CPLUS <= 0x890)
0035 #   define BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT 
0036     namespace std { class bad_typeid { }; }
0037 # endif
0038 
0039 namespace boost
0040 {
0041 
0042   namespace detail
0043   {
0044     //  A separate reporting function was requested during formal review.
0045     inline void report_exception( std::ostream & os, 
0046                                   const char * name, const char * info )
0047       { os << "\n** uncaught exception: " << name << " " << info << std::endl; }
0048   }
0049 
0050   //  catch_exceptions  ------------------------------------------------------//
0051 
0052   template< class Generator >  // Generator is function object returning int
0053   int catch_exceptions( Generator function_object,
0054                         std::ostream & out, std::ostream & err )
0055   {
0056     int result = 0;               // quiet compiler warnings
0057     bool exception_thrown = true; // avoid setting result for each excptn type
0058 
0059 #ifndef BOOST_NO_EXCEPTIONS
0060     try
0061     {
0062 #endif
0063       result = function_object();
0064       exception_thrown = false;
0065 #ifndef BOOST_NO_EXCEPTIONS
0066     }
0067 
0068     //  As a result of hard experience with strangely interleaved output
0069     //  under some compilers, there is a lot of use of endl in the code below
0070     //  where a simple '\n' might appear to do.
0071 
0072     //  The rules for catch & arguments are a bit different from function 
0073     //  arguments (ISO 15.3 paragraphs 18 & 19). Apparently const isn't
0074     //  required, but it doesn't hurt and some programmers ask for it.
0075 
0076     catch ( const char * ex )
0077       { detail::report_exception( out, "", ex ); }
0078     catch ( const std::string & ex )
0079       { detail::report_exception( out, "", ex.c_str() ); }
0080 
0081     //  std:: exceptions
0082     catch ( const std::bad_alloc & ex )
0083       { detail::report_exception( out, "std::bad_alloc:", ex.what() ); }
0084 
0085 # ifndef BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT
0086     catch ( const std::bad_cast & ex )
0087       { detail::report_exception( out, "std::bad_cast:", ex.what() ); }
0088     catch ( const std::bad_typeid & ex )
0089       { detail::report_exception( out, "std::bad_typeid:", ex.what() ); }
0090 # else
0091     catch ( const std::bad_cast & )
0092       { detail::report_exception( out, "std::bad_cast", "" ); }
0093     catch ( const std::bad_typeid & )
0094       { detail::report_exception( out, "std::bad_typeid", "" ); }
0095 # endif
0096 
0097     catch ( const std::bad_exception & ex )
0098       { detail::report_exception( out, "std::bad_exception:", ex.what() ); }
0099     catch ( const std::domain_error & ex )
0100       { detail::report_exception( out, "std::domain_error:", ex.what() ); }
0101     catch ( const std::invalid_argument & ex )
0102       { detail::report_exception( out, "std::invalid_argument:", ex.what() ); }
0103     catch ( const std::length_error & ex )
0104       { detail::report_exception( out, "std::length_error:", ex.what() ); }
0105     catch ( const std::out_of_range & ex )
0106       { detail::report_exception( out, "std::out_of_range:", ex.what() ); }
0107     catch ( const std::range_error & ex )
0108       { detail::report_exception( out, "std::range_error:", ex.what() ); }
0109     catch ( const std::overflow_error & ex )
0110       { detail::report_exception( out, "std::overflow_error:", ex.what() ); }
0111     catch ( const std::underflow_error & ex )
0112       { detail::report_exception( out, "std::underflow_error:", ex.what() ); }
0113     catch ( const std::logic_error & ex )
0114       { detail::report_exception( out, "std::logic_error:", ex.what() ); }
0115     catch ( const std::runtime_error & ex )
0116       { detail::report_exception( out, "std::runtime_error:", ex.what() ); }
0117     catch ( const std::exception & ex )
0118       { detail::report_exception( out, "std::exception:", ex.what() ); }
0119 
0120     catch ( ... )
0121       { detail::report_exception( out, "unknown exception", "" ); }
0122 #endif // BOOST_NO_EXCEPTIONS
0123 
0124     if ( exception_thrown ) result = boost::exit_exception_failure;
0125 
0126     if ( result != 0 && result != exit_success )
0127     {
0128       out << std::endl << "**** returning with error code "
0129                 << result << std::endl;
0130       err
0131         << "**********  errors detected; see stdout for details  ***********"
0132         << std::endl;
0133     }
0134 #if !defined(BOOST_NO_CPP_MAIN_SUCCESS_MESSAGE)
0135     else { out << std::flush << "no errors detected" << std::endl; }
0136 #endif
0137     return result;
0138   } // catch_exceptions
0139 
0140 } // boost
0141 
0142 #endif  // BOOST_CATCH_EXCEPTIONS_HPP
0143