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