Coroutines and exceptions
In the previous sections, we implemented a few basic examples to learn the main C++ coroutines concepts. We implemented a very basic coroutine first to understand what the compiler required from us: the return type (sometimes called the wrapper type because it wraps the promise type) and the promise type.
Even for such a simple coroutine, we had to implement some functions we explained while we wrote the examples. But one function has not been explained yet:
void unhandled_exception() noexcept {}
We assumed then that coroutines couldn’t throw exceptions, but the truth is they can. We can add the functionality to handle exceptions in the body of the unhandled_exception()
function.
Exceptions in coroutines can happen while the return type or the promise type object is created and while the coroutine is executed (as in a normal function, coroutines can throw exceptions).
The difference is that if the exception is thrown before the coroutine...