[asio] Throwing an exception on timeout

Hi folks
While reading out a serial interface with boost asio, I used the following timeout protection:
const boost::function

const boost::function
SerialInterface::ON_TIMEOUT(if_then(_1 != error::operation_aborted, throw_exception(bind(constructor< SerialInterfaceTimeoutException> ())))); // ... void SerialInterface::read(void *buf, size_t size) throw(SerialInterfaceTimeoutException) { charTimeout.expires_from_now(maxCharDelay); charTimeout.async_wait(ON_TIMEOUT); // ... If a timeout occurs, the exception is thrown as expected. However, it is not handed any further from read, but instead the program terminates with a message concerning the exception. Is there any method having a throw() constraint in this async_wait chain?
Your ON_TIMEOUT() function is called from io_service::run(). io_service::run() does not handle your exceptions, so the exception is unhandled and the thread is aborted. By the way, use exception specifications carefully: http://www.gotw.ca/gotw/082.htm

On Sun, Jan 24, 2010 at 9:43 AM, Igor R
const boost::function
SerialInterface::ON_TIMEOUT(if_then(_1 != error::operation_aborted, throw_exception(bind(constructor< SerialInterfaceTimeoutException> ())))); // ... void SerialInterface::read(void *buf, size_t size) throw(SerialInterfaceTimeoutException) { By the way, use exception specifications carefully: http://www.gotw.ca/gotw/082.htm
This advice should be more like "don't ever use exception specifications." :) Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
participants (3)
-
Emil Dotchevski
-
Igor R
-
Pascal Kesseli