在Boost.Asio的异常处理 [英] Exception handling in Boost.Asio

查看:589
本文介绍了在Boost.Asio的异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Boost.Asio的文件<一个href=\"http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/reference/io_service.html#boost_asio.reference.io_service.effect_of_exceptions_thrown_from_handlers\">suggests以下异常处理模式的:

Boost.Asio documentation suggests the following exception handling pattern:

boost::asio::io_service io_service;
...
for (;;)
{
  try
  {
    io_service.run();
    break; // run() exited normally
  }
  catch (my_exception& e)
  {
    // Deal with exception as appropriate.
  }
}

与它的问题是,当它的处理的异常的情况下的点处丢失。举例来说,如果我有一个给定的io_service对象多个套接字会话,我不知道哪一个导致异常。

The problem with it is that the context of exception is lost at the point when it's handled. For example, if I have multiple socket sessions in a given io_service, I don't know which one caused the exception.

什么是更好的方式来处理来自异步处理例外的没有的在包装他们的try / catch 块?

What would be a better way to handle the exceptions from asynchronous handlers without wrapping them in try/catch blocks?

推荐答案

有什么错与Boost.Asio的推荐模式。你应该做的是包装与异常对象一起处理异常的必要信息。如果你使用<一个href=\"http://www.boost.org/doc/libs/1_43_0/libs/exception/doc/boost-exception.html\">boost::exception (或者从它派生的类型),为您的异常处理,你可以很容易通过创建的的boost :: ERROR_INFO 并使用运营商的LT其连接到异常对象;&LT ;.然后,您的catch块可以提取这个信息<一个href=\"http://www.boost.org/doc/libs/1_43_0/libs/exception/doc/get_error_info.html\">get_error_info.

There is nothing wrong with the pattern recommended by Boost.Asio. What you should do is package any necessary information for handling the exception along with the exception object. If you use boost::exception (or a type derived from it) for your exception handling, you can very easily attach metadata (including session information) by creating a specialization of boost::error_info and attaching it to the exception object using operator<<. Your catch block can then extract this info with get_error_info.

这篇关于在Boost.Asio的异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆