catch_exceptions.hpp 5.5 KB

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