std::future_error

From cppreference.com
< cpp‎ | thread
 
 
Thread support library
Threads
(C++11)
this_thread namespace
(C++11)
(C++11)
(C++11)
(C++11)
Mutual exclusion
(C++11)
(C++11)
Generic lock management
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Condition variables
(C++11)
Futures
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
future_error
(C++11)
(C++11)
(C++11)
 
 
Defined in header <future>
class future_error;
(since C++11)

The class std::future_error defines an exception object that is thrown on failure by the functions in the thread library that deal with asynchronous execution and shared states (std::future, std::promise, etc). Similar to std::system_error, this exception carries an error code compatible with std::error_code.

cpp/error/exception cpp/error/logic errorstd-future error-inheritance.svg
About this image

Inheritance diagram

Contents

[edit] Member functions

creates a std::future_error object
(public member function)
returns the error code
(public member function)
returns the explanatory string specific to the error code
(public member function)

Inherited from std::logic_error

Inherited from std::exception

Member functions

[virtual]
destructs the exception object
(virtual public member function of std::exception)
[virtual]
returns an explanatory string
(virtual public member function of std::exception)

[edit] Example

#include <future>
#include <iostream>
 
int main()
{
    std::future<int> empty;
    try {
        int n = empty.get();
    } catch (const std::future_error& e) {
        std::cout << "Caught a future_error with code \"" << e.code()
                  << "\"\nMessage: \"" << e.what() << "\"\n";
    }
}

Output:

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

[edit] See also

(C++11)
identifies the future error codes
(enum)