Namespaces
Variants

std:: future_error

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
(C++11)
future_error
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
定义于头文件 <future>
class future_error ;
(C++11 起)

std::future_error 定义了异常对象,该对象在线程库中处理异步执行和共享状态的函数( std::future std::promise 等)发生故障时被抛出。与 std::system_error 类似,此异常携带一个与 std::error_code 兼容的错误代码。

cpp/error/exception cpp/error/logic error std-future error-inheritance.svg

继承关系图

目录

成员函数

创建 std::future_error 对象
(公开成员函数)
替换 std::future_error 对象
(公开成员函数)
返回错误码
(公开成员函数)
返回特定于错误码的说明字符串
(公开成员函数)

继承自 std:: exception

成员函数

[virtual]
销毁异常对象
( std::exception 的虚公开成员函数)
[virtual]
返回说明性字符串
( std::exception 的虚公开成员函数)

示例

#include <future>
#include <iostream>
int main()
{
    std::future<int> empty;
    try
    {
        int n = empty.get(); // 该行为是未定义的,但
                             // 某些实现会抛出 std::future_error
    }
    catch (const std::future_error& e)
    {
        std::cout << "Caught a future_error with code \"" << e.code()
                  << "\"\nMessage: \"" << e.what() << "\"\n";
    }
}

可能的输出:

Caught a future_error with code "future:3"
Message: "No associated state"

参见

标识 future 错误代码
(枚举)