Python Programming - Generators - Discussion

Discussion Forum : Generators - General Questions (Q.No. 68)
68.
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 stop the generator and raise a StopIteration exception inside the generator. raise StopIteration is used outside the generator to signal the end of the iteration.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.