Skip to content

Why are exceptions in iteration thrown into a generator? #127

Answered by vxgmichel
npt asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @npt

First I want to point out that there are two parts in your original question

where an exception thrown by a loop iterating a generator-backed stream is thrown into the generator

This I'll address later.

and doesn't escape the loop

This one is simple: it doesn't escape the loop because it's not re-raised in yielder. Try this code instead:

@operator
async def yielder():
    try:
        yield 1
        yield 2
        yield 3
    except Exception as e:
        print("Exception in yielder:", e)
        raise

and you'll get the expected behavior.

Now about the first point:

where an exception thrown by a loop iterating a generator-backed stream is thrown into the generator

This b…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by vxgmichel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #126 on January 15, 2025 10:41.