system_error.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_ERROR_HPP
  6. #define BOOST_SYSTEM_ERROR_HPP
  7. #include <string>
  8. #include <stdexcept>
  9. #include <cassert>
  10. #include <boost/system/error_code.hpp>
  11. namespace boost
  12. {
  13. namespace system
  14. {
  15. // class system_error --------------------------------------------------//
  16. class system_error : public std::runtime_error
  17. {
  18. public:
  19. system_error( error_code ec )
  20. : std::runtime_error(""), m_error_code(ec) {}
  21. system_error( error_code ec, const std::string & what_arg )
  22. : std::runtime_error(what_arg), m_error_code(ec) {}
  23. system_error( error_code ec, const char* what_arg )
  24. : std::runtime_error(what_arg), m_error_code(ec) {}
  25. system_error( int ev, const error_category & ecat )
  26. : std::runtime_error(""), m_error_code(ev,ecat) {}
  27. system_error( int ev, const error_category & ecat,
  28. const std::string & what_arg )
  29. : std::runtime_error(what_arg), m_error_code(ev,ecat) {}
  30. system_error( int ev, const error_category & ecat,
  31. const char * what_arg )
  32. : std::runtime_error(what_arg), m_error_code(ev,ecat) {}
  33. virtual ~system_error() throw() {}
  34. const error_code & code() const throw() { return m_error_code; }
  35. const char * what() const throw();
  36. private:
  37. error_code m_error_code;
  38. mutable std::string m_what;
  39. };
  40. // implementation ------------------------------------------------------//
  41. inline const char * system_error::what() const throw()
  42. // see http://www.boost.org/more/error_handling.html for lazy build rationale
  43. {
  44. if ( m_what.empty() )
  45. {
  46. try
  47. {
  48. m_what = this->std::runtime_error::what();
  49. if ( m_error_code )
  50. {
  51. if ( !m_what.empty() ) m_what += ": ";
  52. m_what += m_error_code.message();
  53. }
  54. }
  55. catch (...) { return std::runtime_error::what(); }
  56. }
  57. return m_what.c_str();
  58. }
  59. } // namespace system
  60. } // namespace boost
  61. #endif // BOOST_SYSTEM_ERROR_HPP