catch_exceptions.hpp
Go to the documentation of this file.
00001 //  boost/catch_exceptions.hpp -----------------------------------------------//
00002 
00003 //  Copyright Beman Dawes 1995-2001.  Distributed under the Boost
00004 //  Software License, Version 1.0. (See accompanying file
00005 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
00006 
00007 //  See http://www.boost.org/libs/test for documentation.
00008 
00009 //  Revision History
00010 //   13 Jun 01 report_exception() made inline. (John Maddock, Jesse Jones)
00011 //   26 Feb 01 Numerous changes suggested during formal review. (Beman)
00012 //   25 Jan 01 catch_exceptions.hpp code factored out of cpp_main.cpp.
00013 //   22 Jan 01 Remove test_tools dependencies to reduce coupling.
00014 //    5 Nov 00 Initial boost version (Beman Dawes)
00015 
00016 #ifndef BOOST_CATCH_EXCEPTIONS_HPP
00017 #define BOOST_CATCH_EXCEPTIONS_HPP
00018 
00019 //  header dependencies are deliberately restricted to the standard library
00020 //  to reduce coupling to other boost libraries.
00021 #include <string>             // for string
00022 #include <new>                // for bad_alloc
00023 #include <typeinfo>           // for bad_cast, bad_typeid
00024 #include <exception>          // for exception, bad_exception
00025 #include <stdexcept>          // for std exception hierarchy
00026 #include <boost/cstdlib.hpp>  // for exit codes
00027 # if __GNUC__ != 2 || __GNUC_MINOR__ > 96
00028 #   include <ostream>         // for ostream
00029 # else
00030 #   include <iostream> // workaround GNU missing ostream header
00031 # endif
00032 
00033 # if defined(__BORLANDC__) && (__BORLANDC__ <= 0x0551)
00034 #   define BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT 
00035 # endif
00036 
00037 #if defined(MPW_CPLUS) && (MPW_CPLUS <= 0x890)
00038 #   define BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT 
00039     namespace std { class bad_typeid { }; }
00040 # endif
00041 
00042 namespace boost
00043 {
00044 
00045   namespace detail
00046   {
00047     //  A separate reporting function was requested during formal review.
00048     inline void report_exception( std::ostream & os, 
00049                                   const char * name, const char * info )
00050       { os << "\n** uncaught exception: " << name << " " << info << std::endl; }
00051   }
00052 
00053   //  catch_exceptions  ------------------------------------------------------//
00054 
00055   template< class Generator >  // Generator is function object returning int
00056   int catch_exceptions( Generator function_object,
00057                         std::ostream & out, std::ostream & err )
00058   {
00059     int result = 0;               // quiet compiler warnings
00060     bool exception_thrown = true; // avoid setting result for each excptn type
00061 
00062 #ifndef BOOST_NO_EXCEPTIONS
00063     try
00064     {
00065 #endif
00066       result = function_object();
00067       exception_thrown = false;
00068 #ifndef BOOST_NO_EXCEPTIONS
00069     }
00070 
00071     //  As a result of hard experience with strangely interleaved output
00072     //  under some compilers, there is a lot of use of endl in the code below
00073     //  where a simple '\n' might appear to do.
00074 
00075     //  The rules for catch & arguments are a bit different from function 
00076     //  arguments (ISO 15.3 paragraphs 18 & 19). Apparently const isn't
00077     //  required, but it doesn't hurt and some programmers ask for it.
00078 
00079     catch ( const char * ex )
00080       { detail::report_exception( out, "", ex ); }
00081     catch ( const std::string & ex )
00082       { detail::report_exception( out, "", ex.c_str() ); }
00083 
00084     //  std:: exceptions
00085     catch ( const std::bad_alloc & ex )
00086       { detail::report_exception( out, "std::bad_alloc:", ex.what() ); }
00087 
00088 # ifndef BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT
00089     catch ( const std::bad_cast & ex )
00090       { detail::report_exception( out, "std::bad_cast:", ex.what() ); }
00091     catch ( const std::bad_typeid & ex )
00092       { detail::report_exception( out, "std::bad_typeid:", ex.what() ); }
00093 # else
00094     catch ( const std::bad_cast & )
00095       { detail::report_exception( out, "std::bad_cast", "" ); }
00096     catch ( const std::bad_typeid & )
00097       { detail::report_exception( out, "std::bad_typeid", "" ); }
00098 # endif
00099 
00100     catch ( const std::bad_exception & ex )
00101       { detail::report_exception( out, "std::bad_exception:", ex.what() ); }
00102     catch ( const std::domain_error & ex )
00103       { detail::report_exception( out, "std::domain_error:", ex.what() ); }
00104     catch ( const std::invalid_argument & ex )
00105       { detail::report_exception( out, "std::invalid_argument:", ex.what() ); }
00106     catch ( const std::length_error & ex )
00107       { detail::report_exception( out, "std::length_error:", ex.what() ); }
00108     catch ( const std::out_of_range & ex )
00109       { detail::report_exception( out, "std::out_of_range:", ex.what() ); }
00110     catch ( const std::range_error & ex )
00111       { detail::report_exception( out, "std::range_error:", ex.what() ); }
00112     catch ( const std::overflow_error & ex )
00113       { detail::report_exception( out, "std::overflow_error:", ex.what() ); }
00114     catch ( const std::underflow_error & ex )
00115       { detail::report_exception( out, "std::underflow_error:", ex.what() ); }
00116     catch ( const std::logic_error & ex )
00117       { detail::report_exception( out, "std::logic_error:", ex.what() ); }
00118     catch ( const std::runtime_error & ex )
00119       { detail::report_exception( out, "std::runtime_error:", ex.what() ); }
00120     catch ( const std::exception & ex )
00121       { detail::report_exception( out, "std::exception:", ex.what() ); }
00122 
00123     catch ( ... )
00124       { detail::report_exception( out, "unknown exception", "" ); }
00125 #endif // BOOST_NO_EXCEPTIONS
00126 
00127     if ( exception_thrown ) result = boost::exit_exception_failure;
00128 
00129     if ( result != 0 && result != exit_success )
00130     {
00131       out << std::endl << "**** returning with error code "
00132                 << result << std::endl;
00133       err
00134         << "**********  errors detected; see stdout for details  ***********"
00135         << std::endl;
00136     }
00137 #if !defined(BOOST_NO_CPP_MAIN_SUCCESS_MESSAGE)
00138     else { out << std::flush << "no errors detected" << std::endl; }
00139 #endif
00140     return result;
00141   } // catch_exceptions
00142 
00143 } // boost
00144 
00145 #endif  // BOOST_CATCH_EXCEPTIONS_HPP
00146 


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:28