Python Programming - Generators - Discussion

Discussion Forum : Generators - General Questions (Q.No. 87)
87.
How does the generator.throw(StopIteration) method differ from raise StopIteration when used with a generator?
They have the same effect on the generator
generator.throw(StopIteration) stops the generator and raises a StopIteration exception, while raise StopIteration is used outside the generator
raise StopIteration stops the generator and raises a StopIteration exception, while generator.throw(StopIteration) is used outside the generator
generator.throw(StopIteration) raises a ValueError
Answer: Option
Explanation:
generator.throw(StopIteration) is used to raise a StopIteration exception inside the generator, while raise StopIteration is used outside the generator to signal the end of iteration.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.