Throw exception

Boost.Asio functions may throw boost::system::system_error exception. Take resolve as an example:

  1. results_type resolve(BOOST_ASIO_STRING_VIEW_PARAM host,
  2. BOOST_ASIO_STRING_VIEW_PARAM service, resolver_base::flags resolve_flags)
  3. {
  4. boost::system::error_code ec;
  5. ......
  6. boost::asio::detail::throw_error(ec, "resolve");
  7. return r;
  8. }

There are two overloads of boost::asio::detail::throw_error functions:

  1. inline void throw_error(const boost::system::error_code& err)
  2. {
  3. if (err)
  4. do_throw_error(err);
  5. }
  6. inline void throw_error(const boost::system::error_code& err,
  7. const char* location)
  8. {
  9. if (err)
  10. do_throw_error(err, location);
  11. }

The differences of these two functions is just including “location” (“resolve“ string in our example) or not. Accordingly, do_throw_error also have two overloads, I just take one as an example:

  1. void do_throw_error(const boost::system::error_code& err, const char* location)
  2. {
  3. boost::system::system_error e(err, location);
  4. boost::asio::detail::throw_exception(e);
  5. }

boost::system::system_error derives from std::runtime_error:

  1. class BOOST_SYMBOL_VISIBLE system_error : public std::runtime_error
  2. {
  3. ......
  4. public:
  5. system_error( error_code ec )
  6. : std::runtime_error(""), m_error_code(ec) {}
  7. system_error( error_code ec, const std::string & what_arg )
  8. : std::runtime_error(what_arg), m_error_code(ec) {}
  9. ......
  10. const error_code & code() const BOOST_NOEXCEPT_OR_NOTHROW { return m_error_code; }
  11. const char * what() const BOOST_NOEXCEPT_OR_NOTHROW;
  12. ......
  13. }

what() member function returns the detailed information of exception.