简体   繁体   English

使用Boost ASIO进行Boost异常处理

[英]Boost Exception Handling with Boost ASIO

I was going over this example 5a - it covers exception handling with boost asio The code for the example is pasted here from that link for quick reference 我打算在这个例子5A -它涵盖了异常,并提升ASIO的例子的代码在这里从该链接为快速参考粘贴处理

boost::mutex global_stream_lock;

void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service )
{
    ....

    try
    {
        io_service->run();
    }
    catch( std::exception & ex )
    {
        ....
    }

}

void RaiseAnException( boost::shared_ptr< boost::asio::io_service > io_service )
{
    io_service->post( boost::bind( &RaiseAnException, io_service ) );

    throw( std::runtime_error( "Oops!" ) );
}

int main( int argc, char * argv[] )
{
    boost::shared_ptr< boost::asio::io_service > io_service(
        new boost::asio::io_service
        );
    boost::shared_ptr< boost::asio::io_service::work > work(
        new boost::asio::io_service::work( *io_service )
        );


    boost::thread_group worker_threads;
    for( int x = 0; x < 2; ++x )
    {
        worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );
    }

    io_service->post( boost::bind( &RaiseAnException, io_service ) );

    worker_threads.join_all();

    return 0;
}

My question is why isnt the exception caught here ? 我的问题是为什么这里没有捕获到异常? Why did the author have to do both mechansims error code and try-catch to catch an exception like this 为什么作者必须同时执行mechansims error codetry-catch才能捕获这样的异常

try
        {
            boost::system::error_code ec;
            io_service->run( ec );
            if( ec )
            {
                ....
            }
            break;
        }
        catch( std::exception & ex )
        {
            ....
        } 

I also dont understand what the author means by saying 我也听不懂作者的意思

To further clarify once again if we are using the io_service for user work, we have to use exception handling if the work can generate exceptions. 为了再次澄清我们是否将io_service用于用户工作,如果工作可以生成异常,则必须使用异常处理。 If we are using the io_service for boost::asio functions only, then we can use exception handling or the error variable as either will do. 如果我们仅将io_service用于boost :: asio函数,则可以使用异常处理或error变量,无论哪种情况都可以。 If we are using the io_service for both boost::asio functions and user work, then we can either use both methods or just the exception handling method, but not only the error variable if the work can generate an exception. 如果我们将io_service用于boost :: asio函数和用户工作,则可以使用这两种方法,也可以仅使用异常处理方法,但如果工作可以生成异常,则不仅可以使用错误变量。 That should be pretty straightforward to follow. 这应该很容易理解。

I would appreciate it if someone could clarify this 如果有人可以澄清这一点,我将不胜感激

The explanation you quote is somewhat misleading. 您引用的解释有些误导。

Actually, io_service propagates any exceptions that escape from completion handlers, so it doesn't matter whether we use it for "user work" or for "asio functions" - in any case we might want to handle exceptions escaping from io_service::run (not only std::exception !). 实际上, io_service传播从完成处理程序中逸出的所有异常,因此我们将其用于“用户工作”还是用于“ asio函数”都没有关系-在任何情况下,我们都可能希望处理从io_service::run转义的异常不仅std::exception !)。 Consider the following sample: 考虑以下示例:

void my_handler(const error_code&)
{
  // this exception will escape from io_service::run()!
  throw 0;
}

void setup_timer()
{
  deadline_timer_.expires_from_now(seconds(5));
  deadline_timer_.async_wait(my_handler);
}

The difference between io_service::run(error_code &ec) and io_service::run() is that the latter deliberately throws an exception, if ec implies error. io_service::run(error_code &ec)io_service::run()之间的区别在于,如果ec暗示错误,则后者故意引发异常。 Quoting from io_service.ipp : io_service.ipp引用:

std::size_t io_service::run()
{
  boost::system::error_code ec;
  std::size_t s = impl_.run(ec);
  boost::asio::detail::throw_error(ec);
  return s;
}

So, the bottom line is that it would be enough to use the throwing overload (and, optionally, multiple catch handlers to distinguish between exception types). 因此,最重要的是,使用抛出重载就足够了(并且可以选择使用多个catch处理程序来区分异常类型)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM