system_error.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Boost system_error.hpp --------------------------------------------------//
  2. // Copyright Beman Dawes 2006
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_SYSTEM_SYSTEM_ERROR_HPP
  6. #define BOOST_SYSTEM_SYSTEM_ERROR_HPP
  7. #include <boost/system/error_code.hpp>
  8. #include <string>
  9. #include <stdexcept>
  10. #include <cassert>
  11. namespace boost
  12. {
  13. namespace system
  14. {
  15. // class system_error ------------------------------------------------------------//
  16. class BOOST_SYMBOL_VISIBLE system_error : public std::runtime_error
  17. // BOOST_SYMBOL_VISIBLE is needed by GCC to ensure system_error thrown from a shared
  18. // library can be caught. See svn.boost.org/trac/boost/ticket/3697
  19. {
  20. public:
  21. explicit system_error( error_code ec )
  22. : std::runtime_error(""), m_error_code(ec) {}
  23. system_error( error_code ec, const std::string & what_arg )
  24. : std::runtime_error(what_arg), m_error_code(ec) {}
  25. system_error( error_code ec, const char* what_arg )
  26. : std::runtime_error(what_arg), m_error_code(ec) {}
  27. system_error( int ev, const error_category & ecat )
  28. : std::runtime_error(""), m_error_code(ev,ecat) {}
  29. system_error( int ev, const error_category & ecat,
  30. const std::string & what_arg )
  31. : std::runtime_error(what_arg), m_error_code(ev,ecat) {}
  32. system_error( int ev, const error_category & ecat,
  33. const char * what_arg )
  34. : std::runtime_error(what_arg), m_error_code(ev,ecat) {}
  35. virtual ~system_error() BOOST_NOEXCEPT_OR_NOTHROW {}
  36. error_code code() const BOOST_NOEXCEPT { return m_error_code; }
  37. const char * what() const BOOST_NOEXCEPT_OR_NOTHROW BOOST_OVERRIDE;
  38. private:
  39. error_code m_error_code;
  40. mutable std::string m_what;
  41. };
  42. // implementation ------------------------------------------------------//
  43. inline const char * system_error::what() const BOOST_NOEXCEPT_OR_NOTHROW
  44. // see http://www.boost.org/more/error_handling.html for lazy build rationale
  45. {
  46. if ( m_what.empty() )
  47. {
  48. #ifndef BOOST_NO_EXCEPTIONS
  49. try
  50. #endif
  51. {
  52. m_what = this->std::runtime_error::what();
  53. if ( !m_what.empty() ) m_what += ": ";
  54. m_what += m_error_code.message();
  55. }
  56. #ifndef BOOST_NO_EXCEPTIONS
  57. catch (...) { return std::runtime_error::what(); }
  58. #endif
  59. }
  60. return m_what.c_str();
  61. }
  62. } // namespace system
  63. } // namespace boost
  64. #endif // BOOST_SYSTEM_SYSTEM_ERROR_HPP