命名空间
变体
操作

std::future_error

来自 cppreference.cn
< cpp‎ | thread
 
 
并发支持库
线程
(C++11)
(C++20)
this_thread 命名空间
(C++11)
(C++11)
(C++11)
协同取消
互斥
(C++11)
通用锁管理
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
条件变量
(C++11)
信号量
闩锁和屏障
(C++20)
(C++20)
期物
(C++11)
(C++11)
(C++11)
(C++11)
future_error
(C++11)
安全回收
(C++26)
危害指针
原子类型
(C++11)
(C++20)
原子类型的初始化
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
内存顺序
(C++11)(deprecated in C++26)
原子操作的自由函数
原子标志的自由函数
 
 
定义于头文件 <future>
class future_error;
(自 C++11 起)

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

cpp/error/exceptioncpp/error/logic errorstd-future error-inheritance.svg

继承关系图

目录

[编辑] 成员函数

创建一个 std::future_error 对象
(公共成员函数) [编辑]
替换 std::future_error 对象
(公共成员函数) [编辑]
返回错误代码
(公共成员函数) [编辑]
返回特定于错误代码的解释性字符串
(公共成员函数) [编辑]

继承自 std::logic_error

继承自 std::exception

成员函数

[虚函数]
销毁异常对象
(std::exception 的虚公共成员函数) [编辑]
[虚函数]
返回解释性字符串
(std::exception 的虚公共成员函数) [编辑]

[编辑] 示例

#include <future>
#include <iostream>
 
int main()
{
    std::future<int> empty;
    try
    {
        int n = empty.get(); // The behavior is undefined, but
                             // some implementations throw 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 错误代码
(枚举) [编辑]