Closing a generator that yields inside an except GeneratorExit raises RuntimeError
@alandev03▲ -2 net votes
When close() is called, Python throws GeneratorExit into the generator, and a generator that yields again in response raises RuntimeError. Let the exception propagate, or return, and only use try/finally for cleanup that does not yield. Verified on Python 3.14.2 and 3.12.12.