The GeneratorExit error occurs in Python because of the sudden exit of the generator function. The main difference between Generators and normal functions is that the normal function uses the return keyword to return the values from the function, but in the generator, the yield method is used to return the values.
There are two methods used in the generator function: one is yield, and the another is next. The next method is used to call the next value in the sequence.
Python yield
def generator():
print("Hello this is the 1st element")
yield 5
print("Hello this is the 2nd element")
yield 15
print("Hello this is the 3rd element")
yield 25
print("Hello this is the 4th element")
yield 35
g = generator()
print(next(g))
print(next(g))
Output
Hello this is the 1st element
5
Hello this is the 2nd element
15
Here we saw how to work with yield and the next methods. In this example, we created a generator function in which we have yielded 4 values. If the return statement is executed in a normal function, the remaining statements in functions are not implemented. Instead, the function is returned to the calling statement. But while using yield, the overall function is executed, and the results are stored in the memory.
We created an object named g, calling the next(g). Then, the next method calls the next element in the returned value.
Let’s see another example.
def sample():
arr = [1, 2, 3, 4, 5]
for i in arr:
yield i
print("From Function: ", i)
print("Reached the end!!!")
for num in sample():
print("Output: ", num)
print("Thank You")
Output
Output: 1
From Function: 1
Output: 2
From Function: 2
Output: 3
From Function: 3
Output: 4
From Function: 4
Output: 5
From Function: 5
Reached the end!!!
Thank You
This works fine, and we can see the output of how well the program has been executed. This is a remarkable feature of generators.
GeneratorExit in Python
GeneratorExit error in Python occurs when you have a generator that yields things. After each yield, you want to execute some code that does something like logging or cleaning up.
def sample():
arr = [1, 2, 3, 4, 5]
for i in arr:
yield i
print("From Function: ", i)
print("Reached the end!!!")
for num in sample():
print("Output: ", num)
if(num >= 3):
break
print("Thank You")
Output
Output: 1
From Function: 1
Output: 2
From Function: 2
Output: 3
Thank You
If we execute this program the output is generated. But there is one problem with that the problem is called the GeneratorExit. This is created because we terminated the program in the middle by using a break statement. To overcome this we can use the try-except block.
def sample():
arr = [1, 2, 3, 4, 5]
try:
for i in arr:
yield i
print("From Function: ", i)
except GeneratorExit:
print("Exception has Occured")
print("Reached the end!!!")
for num in sample():
print("Output:", num)
if(num >= 3):
break
print("Thank You")
Output
Output: 1
From Function: 1
Output: 2
From Function: 2
Output: 3
Exception has Occured
Reached the end!!!
Thank You
Conclusion
The GeneratorExit error is caused due to the sudden termination of the generator function. This can be handled by using the try and except clause.
That’s it for this tutorial.
Related posts

Krunal Lathiya is an Information Technology Engineer. By profession, he is a web developer with knowledge of multiple back-end platforms including Python. Krunal has written many programming blogs which showcases his vast knowledge in this field.