如果 std::async 調用的函數拋出異常,那么這個異常會被存儲在值的位置,同時 future 變為 ready ,如果調用 get() 會重新拋出存儲的異常。
Note:
標準并沒有指定原來的異常對象是被重新拋出或者拷貝后拋出,不同的編譯器會做不同的選擇。
對于 std::packaged_task<> 包裝的函數如果拋出異常,那么處理與 std::async 一致。 std::promise<> 提供類似的機制,如果期望存儲一個異常那么在 std::promise<> 對象上就需要使用 set_exception() 代替 set_value() 。
保存一個異常到 future 中的另外一個方式是在沒有調用 std::promise<> 的 set_xxxx 函數或者沒有執行被 std::packaged_task<> 包裝的任務的情況下就銷毀與 future 關聯的 std::promise<> 和 std::packaged_task<>。無論哪種情況,析構函數都會存儲一個錯誤碼是 std::future_errc::broken_promise 的 std::future_error 異常,表示關聯的 future 并沒有 ready 。
#include <iostream>
#include <future>int main() {// std::asyncstd::future<void> result1 = std::async([] { throw std::runtime_error("runtime error."); });try {result1.get();} catch (const std::runtime_error &e) {std::cout << "Async exception: " << e.what() << std::endl;}std::promise<int> promise;try {// 正常的情況就set_value
// promise.set_value(66);// 主動拋出一個異常進行測試throw std::out_of_range("the value out of range.");} catch (...) {// 使用std::current_exception 存儲被拋出的異常;// 可以使用 std::copy_exception 存儲沒有被拋出的異常;promise.set_exception(std::current_exception());}try {promise.get_future().get();} catch (const std::out_of_range &e) {std::cout << "Promise exception: " << e.what() << std::endl;}// 存儲異常的其他方式,// 在沒有調用 std::promise的set_xxx函數// 或者沒有調用被 std::packaged_task 包裝的任務情況下// 就銷毀與 future關聯的 std::promise,std::packaged_task 對象// std::packaged_task<>std::future<void> future;try {// 提前銷毀task{std::packaged_task<void()> task([] {std::cout << "do packaged task." << std::endl;});future = task.get_future();}future.get();}catch (const std::future_error &e) {std::cout << "Packaged task exception: " << e.what() << std::endl;}// std::promise<>try {// 提前銷毀promise{std::promise<void> promise;future = promise.get_future();}future.get();}catch (const std::future_error &e) {std::cout << "Promise exception: " << e.what() << std::endl;}return 0;
}
更多內容:https://blog.nas-kk.top/?p=106