問題描述
boost::asio::io_service::run()
在出錯時拋出 boost::system::system_error
異常.我應(yīng)該處理這個異常嗎?如果是,怎么辦?
boost::asio::io_service::run()
throws a boost::system::system_error
exception in case of error. Should I handle this exception? If so, how?
我的 main.cpp 代碼是這樣的:
my main.cpp code is something like this:
main()
{
boost::asio::io_service queue;
boost::asio::io_service::work work(queue);
{
// set some handlers...
**queue.run();**
}
// join some workers...
return 0;
}
推薦答案
是.
據(jù)記載,從完成處理程序拋出的異常會被傳播.因此,您需要根據(jù)您的應(yīng)用程序適當(dāng)?shù)靥幚硭鼈?
It is documented that exceptions thrown from completion handlers are propagated. So you need to handle them as appropriate for your application.
在許多情況下,這會循環(huán)并重復(fù) run()
直到它無錯誤退出.
In many cases, this would be looping and repeating the run()
until it exits without an error.
在我們的代碼庫中,我有類似的東西
In our code base I have something like
static void m_asio_event_loop(boost::asio::io_service& svc, std::string name) {
// http://www.boost.org/doc/libs/1_61_0/doc/html/boost_asio/reference/io_service.html#boost_asio.reference.io_service.effect_of_exceptions_thrown_from_handlers
for (;;) {
try {
svc.run();
break; // exited normally
} catch (std::exception const &e) {
logger.log(LOG_ERR) << "[eventloop] An unexpected error occurred running " << name << " task: " << e.what();
} catch (...) {
logger.log(LOG_ERR) << "[eventloop] An unexpected error occurred running " << name << " task";
}
}
}
這是文檔鏈接 http://www.boost.org/doc/libs/1_61_0/doc/html/boost_asio/reference/io_service.html#boost_asio.reference.io_service.effect_of_exceptions_thrown_from_handlers
這篇關(guān)于應(yīng)該捕獲 boost::asio::io_service::run() 拋出的異常嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!