future_error — std::future_error
future_error — std::future_error
std::future_error 클래스는 비동기 실행과 공유 상태(std::future, std::promise 등)를 다루는 스레드 라이브러리 함수가 실패할 때 던져지는 예외 객체를 정의해요. C++11에서 도입됐어요. <future> 헤더에 있어요.
std::system_error와 유사하게 오류 코드를 담아요.
출처: cppreference
본문
// <future> 헤더, C++11
class future_error;
std::future_error는 future/promise의 잘못된 사용 시 던져져요. .code()로 std::future_errc 오류 코드를 얻을 수 있어요.
사용 예
#include <future>
#include <iostream>
std::promise<int> p;
auto f = p.get_future();
p.set_value(42);
try {
auto f2 = p.get_future(); // 이미 get_future 호출 → future_already_retrieved
} catch (const std::future_error& e) {
std::cout << "error: " << e.what() << '\n';
std::cout << "code: " << static_cast<int>(e.code().value()) << '\n';
}
오류 코드 종류
std::future_error가 담는 std::future_errc 값들:
broken_promisefuture_already_retrievedpromise_already_satisfiedno_state
try {
std::future<int> f; // 공유 상태 없음
f.get(); // no_state
} catch (const std::future_error& e) {
if (e.code() == std::make_error_code(std::future_errc::no_state)) {
std::cout << "no state\n";
}
}
특징
std::system_error에서 파생되므로e.code(),e.what()사용 가능.- 비동기 결과 라이브러리의 표준 실패 신호예요.
- 잘못된 future/promise 사용을 잡아내는 데 필수.
std::future_error는 std::future/std::promise 계열의 오류 상황(상태 재접근, 상태 없음 등)을 안전하게 처리할 때 잡아야 하는 예외예요.