catch_exceptions.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // boost/catch_exceptions.hpp -----------------------------------------------//
  2. // Copyright Beman Dawes 1995-2001. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for documentation.
  6. // Revision History
  7. // 13 Jun 01 report_exception() made inline. (John Maddock, Jesse Jones)
  8. // 26 Feb 01 Numerous changes suggested during formal review. (Beman)
  9. // 25 Jan 01 catch_exceptions.hpp code factored out of cpp_main.cpp.
  10. // 22 Jan 01 Remove test_tools dependencies to reduce coupling.
  11. // 5 Nov 00 Initial boost version (Beman Dawes)
  12. #ifndef BOOST_CATCH_EXCEPTIONS_HPP
  13. #define BOOST_CATCH_EXCEPTIONS_HPP
  14. // header dependencies are deliberately restricted to the standard library
  15. // to reduce coupling to other boost libraries.
  16. #include <string> // for string
  17. #include <new> // for bad_alloc
  18. #include <typeinfo> // for bad_cast, bad_typeid
  19. #include <exception> // for exception, bad_exception
  20. #include <stdexcept> // for std exception hierarchy
  21. #include <boost/cstdlib.hpp> // for exit codes
  22. # if __GNUC__ != 2 || __GNUC_MINOR__ > 96
  23. # include <ostream> // for ostream
  24. # else
  25. # include <iostream> // workaround GNU missing ostream header
  26. # endif
  27. # if defined(__BORLANDC__) && (__BORLANDC__ <= 0x0551)
  28. # define BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT
  29. # endif
  30. #if defined(MPW_CPLUS) && (MPW_CPLUS <= 0x890)
  31. # define BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT
  32. namespace std { class bad_typeid { }; }
  33. # endif
  34. namespace boost
  35. {
  36. namespace detail
  37. {
  38. // A separate reporting function was requested during formal review.
  39. inline void report_exception( std::ostream & os,
  40. const char * name, const char * info )
  41. { os << "\n** uncaught exception: " << name << " " << info << std::endl; }
  42. }
  43. // catch_exceptions ------------------------------------------------------//
  44. template< class Generator > // Generator is function object returning int
  45. int catch_exceptions( Generator function_object,
  46. std::ostream & out, std::ostream & err )
  47. {
  48. int result = 0; // quiet compiler warnings
  49. bool exception_thrown = true; // avoid setting result for each excptn type
  50. #ifndef BOOST_NO_EXCEPTIONS
  51. try
  52. {
  53. #endif
  54. result = function_object();
  55. exception_thrown = false;
  56. #ifndef BOOST_NO_EXCEPTIONS
  57. }
  58. // As a result of hard experience with strangely interleaved output
  59. // under some compilers, there is a lot of use of endl in the code below
  60. // where a simple '\n' might appear to do.
  61. // The rules for catch & arguments are a bit different from function
  62. // arguments (ISO 15.3 paragraphs 18 & 19). Apparently const isn't
  63. // required, but it doesn't hurt and some programmers ask for it.
  64. catch ( const char * ex )
  65. { detail::report_exception( out, "", ex ); }
  66. catch ( const std::string & ex )
  67. { detail::report_exception( out, "", ex.c_str() ); }
  68. // std:: exceptions
  69. catch ( const std::bad_alloc & ex )
  70. { detail::report_exception( out, "std::bad_alloc:", ex.what() ); }
  71. # ifndef BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT
  72. catch ( const std::bad_cast & ex )
  73. { detail::report_exception( out, "std::bad_cast:", ex.what() ); }
  74. catch ( const std::bad_typeid & ex )
  75. { detail::report_exception( out, "std::bad_typeid:", ex.what() ); }
  76. # else
  77. catch ( const std::bad_cast & )
  78. { detail::report_exception( out, "std::bad_cast", "" ); }
  79. catch ( const std::bad_typeid & )
  80. { detail::report_exception( out, "std::bad_typeid", "" ); }
  81. # endif
  82. catch ( const std::bad_exception & ex )
  83. { detail::report_exception( out, "std::bad_exception:", ex.what() ); }
  84. catch ( const std::domain_error & ex )
  85. { detail::report_exception( out, "std::domain_error:", ex.what() ); }
  86. catch ( const std::invalid_argument & ex )
  87. { detail::report_exception( out, "std::invalid_argument:", ex.what() ); }
  88. catch ( const std::length_error & ex )
  89. { detail::report_exception( out, "std::length_error:", ex.what() ); }
  90. catch ( const std::out_of_range & ex )
  91. { detail::report_exception( out, "std::out_of_range:", ex.what() ); }
  92. catch ( const std::range_error & ex )
  93. { detail::report_exception( out, "std::range_error:", ex.what() ); }
  94. catch ( const std::overflow_error & ex )
  95. { detail::report_exception( out, "std::overflow_error:", ex.what() ); }
  96. catch ( const std::underflow_error & ex )
  97. { detail::report_exception( out, "std::underflow_error:", ex.what() ); }
  98. catch ( const std::logic_error & ex )
  99. { detail::report_exception( out, "std::logic_error:", ex.what() ); }
  100. catch ( const std::runtime_error & ex )
  101. { detail::report_exception( out, "std::runtime_error:", ex.what() ); }
  102. catch ( const std::exception & ex )
  103. { detail::report_exception( out, "std::exception:", ex.what() ); }
  104. catch ( ... )
  105. { detail::report_exception( out, "unknown exception", "" ); }
  106. #endif // BOOST_NO_EXCEPTIONS
  107. if ( exception_thrown ) result = boost::exit_exception_failure;
  108. if ( result != 0 && result != exit_success )
  109. {
  110. out << std::endl << "**** returning with error code "
  111. << result << std::endl;
  112. err
  113. << "********** errors detected; see stdout for details ***********"
  114. << std::endl;
  115. }
  116. #if !defined(BOOST_NO_CPP_MAIN_SUCCESS_MESSAGE)
  117. else { out << std::flush << "no errors detected" << std::endl; }
  118. #endif
  119. return result;
  120. } // catch_exceptions
  121. } // boost
  122. #endif // BOOST_CATCH_EXCEPTIONS_HPP