在c++++协程中处理异常的关键在于利用try...catch块捕获异常、处理co_await表达式抛出的异常,并通过promise对象的unhandled_exception()方法管理未处理异常。1. 在协程体内直接使用try...catch捕获并处理异常;2. co_await表达式若引发异常,会传播至协程体,需在try...catch中捕获;3. 未处理的异常会调用promise的unhandled_exception()方法,默认终止程序,可自定义保存异常;4. 异常可在协程链中通过set_exception传递;5. 使用raii或智能指针避免异常导致资源泄漏。

在C++协程中处理异常,本质上就是在协程帧的生命周期内,确保即使发生错误,程序也能优雅地恢复或终止,避免崩溃。 这需要我们理解协程的挂起和恢复机制,并巧妙地利用try...catch块。

协程帧中的异常捕获与传播,主要依赖于co_await操作符和promise对象。 当协程体内部抛出异常时,这个异常需要能够被“传递”到调用者或者协程的promise对象,以便进行处理。
处理协程中的异常,需要关注几个关键点:一是协程体内的异常捕获,二是co_await表达式中的异常处理,三是promise对象的异常处理机制。
立即进入“豆包AI人工智官网入口”;
立即学习“豆包AI人工智能在线问答入口”;

最直接的方式,就是在协程函数体内使用try...catch块。 这和普通的C++函数异常处理没有本质区别,但需要注意的是,协程可能会挂起,因此异常发生的位置和处理的位置可能不在同一个函数调用栈上。
例如:

#include <iostream>
#include <coroutine>
struct MyCoroutine {
struct promise_type {
int result;
MyCoroutine get_return_object() { return {std::coroutine_handle<promise_type>::from_promise(*this)}; }
std::suspend_never initial_suspend() { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void unhandled_exception() { std::terminate(); }
void return_value(int value) { result = value; }
};
std::coroutine_handle<promise_type> handle;
};
MyCoroutine my_coroutine() {
try {
// 模拟可能抛出异常的操作
throw std::runtime_error("Something went wrong!");
co_return 42; // 如果没有异常,返回42
} catch (const std::exception& e) {
std::cerr << "Caught exception in coroutine: " << e.what() << std::endl;
co_return -1; // 发生异常时,返回-1
}
}
int main() {
MyCoroutine coro = my_coroutine();
std::cout << "Result: " << coro.handle.promise().result << std::endl;
coro.handle.destroy();
return 0;
}在这个例子中,如果throw std::runtime_error被执行,那么异常会被catch块捕获,并打印错误信息。 协程最终会co_return -1,将-1作为结果返回。
co_await 表达式如何处理异常?当co_await一个可能抛出异常的awaitable对象时,异常的处理会稍微复杂一些。 如果awaitable对象的await_resume()方法抛出异常,那么这个异常会被传播到协程中,就像co_await表达式直接抛出异常一样。
#include <iostream>
#include <coroutine>
#include <stdexcept>
struct ExceptionAwaitable {
bool await_ready() { return false; }
void await_suspend(std::coroutine_handle<> h) { h.resume(); }
int await_resume() {
throw std::runtime_error("Exception from await_resume!");
}
};
struct MyCoroutine2 {
struct promise_type {
int result;
MyCoroutine2 get_return_object() { return {std::coroutine_handle<promise_type>::from_promise(*this)}; }
std::suspend_never initial_suspend() { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void unhandled_exception() { std::terminate(); }
void return_value(int value) { result = value; }
};
std::coroutine_handle<promise_type> handle;
};
MyCoroutine2 my_coroutine2() {
try {
int value = co_await ExceptionAwaitable{};
std::cout << "Value after co_await: " << value << std::endl; // 不会被执行
co_return 42;
} catch (const std::exception& e) {
std::cerr << "Caught exception after co_await: " << e.what() << std::endl;
co_return -1;
}
}
int main() {
MyCoroutine2 coro = my_coroutine2();
std::cout << "Result: " << coro.handle.promise().result << std::endl;
coro.handle.destroy();
return 0;
}在这个例子中,ExceptionAwaitable的await_resume()方法会抛出一个异常。 这个异常会被my_coroutine2函数中的try...catch块捕获。
unhandled_exception()方法有什么作用?如果协程体内的异常没有被捕获,那么会调用promise对象的unhandled_exception()方法。 默认情况下,这个方法会调用std::terminate(),导致程序终止。 我们可以自定义这个方法,来实现更灵活的异常处理。
例如,我们可以记录错误信息,或者尝试恢复协程的状态。
#include <iostream>
#include <coroutine>
#include <exception>
struct MyCoroutine3 {
struct promise_type {
int result;
std::exception_ptr exception;
MyCoroutine3 get_return_object() { return {std::coroutine_handle<promise_type>::from_promise(*this)}; }
std::suspend_never initial_suspend() { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void unhandled_exception() {
exception = std::current_exception(); // 保存异常
}
void return_value(int value) { result = value; }
};
std::coroutine_handle<promise_type> handle;
};
MyCoroutine3 my_coroutine3() {
throw std::runtime_error("Unhandled exception in coroutine!");
co_return 42; // 不会被执行
}
int main() {
MyCoroutine3 coro = my_coroutine3();
if (coro.handle.promise().exception) {
try {
std::rethrow_exception(coro.handle.promise().exception);
} catch (const std::exception& e) {
std::cerr << "Caught unhandled exception in main: " << e.what() << std::endl;
}
} else {
std::cout << "Result: " << coro.handle.promise().result << std::endl;
}
coro.handle.destroy();
return 0;
}在这个例子中,my_coroutine3函数会抛出一个未处理的异常。 这个异常会被promise对象的unhandled_exception()方法捕获,并保存到exception成员中。 在main函数中,我们可以检查exception成员是否为空,如果不为空,则重新抛出异常并进行处理。
co_await链中传递异常?在复杂的协程调用链中,异常需要在不同的协程之间传递。 这可以通过promise对象的return_void()和set_exception()方法来实现。
当一个协程需要将异常传递给它的调用者时,它可以调用promise对象的set_exception()方法。 这个方法会将异常保存到promise对象中,并在协程恢复时重新抛出。
#include <iostream>
#include <coroutine>
#include <stdexcept>
struct NestedCoroutine {
struct promise_type {
NestedCoroutine get_return_object() { return {std::coroutine_handle<promise_type>::from_promise(*this)}; }
std::suspend_never initial_suspend() { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void unhandled_exception() { std::terminate(); }
void return_void() {} // 协程不返回值时使用
void set_exception(std::exception_ptr e) {
exception = e;
}
std::exception_ptr exception;
};
std::coroutine_handle<promise_type> handle;
};
NestedCoroutine nested_coroutine() {
throw std::runtime_error("Exception from nested coroutine!");
co_return; // 不会被执行
}
struct MyCoroutine4 {
struct promise_type {
int result;
MyCoroutine4 get_return_object() { return {std::coroutine_handle<promise_type>::from_promise(*this)}; }
std::suspend_never initial_suspend() { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void unhandled_exception() { std::terminate(); }
void return_value(int value) { result = value; }
};
std::coroutine_handle<promise_type> handle;
};
MyCoroutine4 my_coroutine4() {
try {
co_await nested_coroutine();
co_return 42; // 不会被执行
} catch (const std::exception& e) {
std::cerr << "Caught exception in my_coroutine4: " << e.what() << std::endl;
co_return -1;
}
}
int main() {
MyCoroutine4 coro = my_coroutine4();
std::cout << "Result: " << coro.handle.promise().result << std::endl;
coro.handle.destroy();
return 0;
}在这个例子中,nested_coroutine函数会抛出一个异常。 这个异常会被传递到my_coroutine4函数中,并被try...catch块捕获。
在使用协程时,需要特别注意资源管理。 如果在协程挂起时发生异常,那么可能会导致资源泄漏。 为了避免这种情况,可以使用RAII(Resource Acquisition Is Initialization)技术,或者使用智能指针来管理资源。
另外,也可以在promise对象的final_suspend()方法中释放资源。 这个方法会在协程结束时被调用,无论协程是正常结束还是因为异常而结束。
以上就是怎样在C++协程中处理异常 协程帧中的异常捕获与传播的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号