简体   繁体   中英

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

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

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. 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. 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. 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 !). 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. Quoting from 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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