如何将异常报告给boost::future
How to get the exception reported to boost::future?
如果我使用Boost期货,并且未来报告为has_exception(),是否有任何方法检索该异常?例如,下面是代码:
int do_something() {
...
throw some_exception();
...
}
...
boost::packaged_task task(do_something);
boost::unique_future<int> fi=task.get_future();
boost::thread thread(boost::move(task));
fi.wait();
if (fi.has_exception()) {
boost::rethrow_exception(?????);
}
...
问题是,应该用什么来代替"?????"?
根据http://groups.google.com/group/boost-list/browse_thread/thread/1340bf8190eec9d9?fwc=2,您需要这样做:
#include <boost/throw_exception.hpp>
int do_something() {
...
BOOST_THROW_EXCEPTION(some_exception());
...
}
...
try
{
boost::packaged_task task(do_something);
boost::unique_future<int> fi=task.get_future();
boost::thread thread(boost::move(task));
int answer = fi.get();
}
catch(const some_exception&)
{ cout<< "caught some_exception" << endl;}
catch(const std::exception& err)
{/*....*/}
...
相关文章:
- 如何使用 boost::future 重新抛出 std::exception_ptr 存储的原始异常?
- boost::future::那它是如何实现的?
- BOOST :: Future延期延续的持续僵局
- boost::future .then() 继续返回 boost::future
- 将 Boost 链与 std::future 结合使用
- 程序在使用异步的 Boost.Process 调用 std::future.get() 后挂起
- 成员std::future防止boost::shared_ptr超出范围
- 异常不会使用boost :: future/boost :: Promise跨线程正确传播
- 使用 boost::future .then() 时出现编译错误
- boost的类型::future<> from boost::async()
- (如何)当std::future准备就绪时,我可以获得boost::asio::io_service的回调吗
- boost::packaged_task生成的future是否抛出错误的异常
- boost::future-是否保证wait_callback只被调用一次
- boost::asio+std::future-关闭套接字后发生访问冲突
- 在主线程中执行,boost::future::then
- 如何将异常报告给boost::future
- 为什么 boost::future:<T>:then() 会生成一个新线程?
- 在continuation和boost::when_all中使用boost::future
- c++ 17 resume /await:它也可以与boost::future一起使用吗?
- Boost::future和continuations -值集,但future仍然阻塞