Should the exception thrown by boost::asio::io_service::run() be caught?(boost::asio::io_service::run() 抛出的异常是否应该被捕获?)
问题描述
boost::asio::io_service::run()
在发生错误时抛出 boost::system::system_error
异常.我应该处理这个异常吗?如果是,怎么办?
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;
}
推荐答案
是的.
据记载,完成处理程序抛出的异常会被传播.所以你需要根据你的应用来处理它们.
It is documented that exceptions thrown from completion handlers are propagated. So you need to handle them as appropriate for your application.
在许多情况下,这会循环并重复 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_throw_from_handlers
这篇关于boost::asio::io_service::run() 抛出的异常是否应该被捕获?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:boost::asio::io_service::run() 抛出的异常是否应该被捕获?
基础教程推荐
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17