gpt4 book ai didi

c++ - 促进 ASIO 异常传播

转载 作者:行者123 更新时间:2023-11-28 01:58:20 26 4
gpt4 key购买 nike

<分区>

我注意到在许多 Boost ASIO 示例中,正在调用可能引发错误的函数,但没有使用 try/catch。例如,阻塞 UDP 客户端示例 here具有以下功能:

  void check_deadline()
{
// Check whether the deadline has passed. We compare the deadline against
// the current time since a new asynchronous operation may have moved the
// deadline before this actor had a chance to run.
if (deadline_.expires_at() <= deadline_timer::traits_type::now())
{
// The deadline has passed. The outstanding asynchronous operation needs
// to be cancelled so that the blocked receive() function will return.
//
// Please note that cancel() has portability issues on some versions of
// Microsoft Windows, and it may be necessary to use close() instead.
// Consult the documentation for cancel() for further information.
socket_.cancel();

// There is no longer an active deadline. The expiry is set to positive
// infinity so that the actor takes no action until a new deadline is set.
deadline_.expires_at(boost::posix_time::pos_infin);
}

// Put the actor back to sleep.
deadline_.async_wait(boost::bind(&client::check_deadline, this));
}

deadline_.expires_at ( here ) 的文档声明此函数抛出 boost::system::system_error 异常。

在这个例子中没有捕获到它是因为它只是一个例子,还是像这样的调用抛出的异常通过调用 run、run-one 等向上传播?换句话说,用 try catch 包装对 io_service.run() 的调用是否足以处理这些类型的异常?

此外,我还注意到 deadline_.async_wait 文档 here指出处理程序需要一个引用 boost::system::system_error::error_code 的签名。我在 check_deadline() 函数中没有看到引用或占位符。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com