Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef BOOST_CATCH_EXCEPTIONS_HPP
00017 #define BOOST_CATCH_EXCEPTIONS_HPP
00018
00019
00020
00021 #include <string>
00022 #include <new>
00023 #include <typeinfo>
00024 #include <exception>
00025 #include <stdexcept>
00026 #include <boost/cstdlib.hpp>
00027 # if __GNUC__ != 2 || __GNUC_MINOR__ > 96
00028 # include <ostream>
00029 # else
00030 # include <iostream>
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
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
00054
00055 template< class Generator >
00056 int catch_exceptions( Generator function_object,
00057 std::ostream & out, std::ostream & err )
00058 {
00059 int result = 0;
00060 bool exception_thrown = true;
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
00072
00073
00074
00075
00076
00077
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
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 }
00142
00143 }
00144
00145 #endif // BOOST_CATCH_EXCEPTIONS_HPP
00146