catch_exceptions.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <ostream> // for ostream
  19. #include <typeinfo> // for bad_cast, bad_typeid
  20. #include <exception> // for exception, bad_exception
  21. #include <stdexcept> // for std exception hierarchy
  22. #include <boost/config.hpp>
  23. #include <boost/cstdlib.hpp> // for exit codes
  24. # if defined(BOOST_BORLANDC) && (__BORLANDC__ <= 0x0551)
  25. # define BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT
  26. # endif
  27. #if defined(MPW_CPLUS) && (MPW_CPLUS <= 0x890)
  28. # define BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT
  29. namespace std { class bad_typeid { }; }
  30. # endif
  31. namespace boost
  32. {
  33. namespace detail
  34. {
  35. // A separate reporting function was requested during formal review.
  36. inline void report_exception( std::ostream & os,
  37. const char * name, const char * info )
  38. { os << "\n** uncaught exception: " << name << " " << info << std::endl; }
  39. }
  40. // catch_exceptions ------------------------------------------------------//
  41. template< class Generator > // Generator is function object returning int
  42. int catch_exceptions( Generator function_object,
  43. std::ostream & out, std::ostream & err )
  44. {
  45. int result = 0; // quiet compiler warnings
  46. bool exception_thrown = true; // avoid setting result for each excptn type
  47. #ifndef BOOST_NO_EXCEPTIONS
  48. try
  49. {
  50. #endif
  51. result = function_object();
  52. exception_thrown = false;
  53. #ifndef BOOST_NO_EXCEPTIONS
  54. }
  55. // As a result of hard experience with strangely interleaved output
  56. // under some compilers, there is a lot of use of endl in the code below
  57. // where a simple '\n' might appear to do.
  58. // The rules for catch & arguments are a bit different from function
  59. // arguments (ISO 15.3 paragraphs 18 & 19). Apparently const isn't
  60. // required, but it doesn't hurt and some programmers ask for it.
  61. catch ( const char * ex )
  62. { detail::report_exception( out, "", ex ); }
  63. catch ( const std::string & ex )
  64. { detail::report_exception( out, "", ex.c_str() ); }
  65. // std:: exceptions
  66. catch ( const std::bad_alloc & ex )
  67. { detail::report_exception( out, "std::bad_alloc:", ex.what() ); }
  68. # ifndef BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT
  69. catch ( const std::bad_cast & ex )
  70. { detail::report_exception( out, "std::bad_cast:", ex.what() ); }
  71. catch ( const std::bad_typeid & ex )
  72. { detail::report_exception( out, "std::bad_typeid:", ex.what() ); }
  73. # else
  74. catch ( const std::bad_cast & )
  75. { detail::report_exception( out, "std::bad_cast", "" ); }
  76. catch ( const std::bad_typeid & )
  77. { detail::report_exception( out, "std::bad_typeid", "" ); }
  78. # endif
  79. catch ( const std::bad_exception & ex )
  80. { detail::report_exception( out, "std::bad_exception:", ex.what() ); }
  81. catch ( const std::domain_error & ex )
  82. { detail::report_exception( out, "std::domain_error:", ex.what() ); }
  83. catch ( const std::invalid_argument & ex )
  84. { detail::report_exception( out, "std::invalid_argument:", ex.what() ); }
  85. catch ( const std::length_error & ex )
  86. { detail::report_exception( out, "std::length_error:", ex.what() ); }
  87. catch ( const std::out_of_range & ex )
  88. { detail::report_exception( out, "std::out_of_range:", ex.what() ); }
  89. catch ( const std::range_error & ex )
  90. { detail::report_exception( out, "std::range_error:", ex.what() ); }
  91. catch ( const std::overflow_error & ex )
  92. { detail::report_exception( out, "std::overflow_error:", ex.what() ); }
  93. catch ( const std::underflow_error & ex )
  94. { detail::report_exception( out, "std::underflow_error:", ex.what() ); }
  95. catch ( const std::logic_error & ex )
  96. { detail::report_exception( out, "std::logic_error:", ex.what() ); }
  97. catch ( const std::runtime_error & ex )
  98. { detail::report_exception( out, "std::runtime_error:", ex.what() ); }
  99. catch ( const std::exception & ex )
  100. { detail::report_exception( out, "std::exception:", ex.what() ); }
  101. catch ( ... )
  102. { detail::report_exception( out, "unknown exception", "" ); }
  103. #endif // BOOST_NO_EXCEPTIONS
  104. if ( exception_thrown ) result = boost::exit_exception_failure;
  105. if ( result != 0 && result != exit_success )
  106. {
  107. out << std::endl << "**** returning with error code "
  108. << result << std::endl;
  109. err
  110. << "********** errors detected; see stdout for details ***********"
  111. << std::endl;
  112. }
  113. #if !defined(BOOST_NO_CPP_MAIN_SUCCESS_MESSAGE)
  114. else { out << std::flush << "no errors detected" << std::endl; }
  115. #endif
  116. return result;
  117. } // catch_exceptions
  118. } // boost
  119. #endif // BOOST_CATCH_EXCEPTIONS_HPP