Python Forum
raise exception within generator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
raise exception within generator
#1
Hi,

I have a generator

def myRange(start, end):
    while start < end:
          yield start
          start += 1
    raise StopIteration

for n in myRange(2, 5):
    print(n)
I get a runtime error generator raised StopIteration. If I remove the raising of the StopIteration it works fine.

I thought that the for would catch the StopIteration exception. myRange is an iterator as well as being a generator and the for would catch the StopIteration exception. Can't generators raise exceptions?
Reply
#2
You don't need to raise that StopIteration.
def myRange(start, end):
    while start < end:
        yield start
        start += 1
 
for n in myRange(2, 5):
    print(n)

spam = myRange(2, 5)
while True:
    print(next(spam))
Output:
2 3 4 2 3 4 Traceback (most recent call last): File "***", line 11, in <module> print(next(spam)) StopIteration
as you can see, it will raise it when needed.
In your code it handles the StopIteration when generator is exhausted, but then you raise the exception that is not handled
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks for the reply.

According to the link below one has to handle any StopIteration exceptions one generates within the
generator itself. If one does not and lets the exception bubble out from the generator then Python turns it into a RuntimeError.

https://www.python.org/dev/peps/pep-0479/
Reply
#4
your understanding is correct - this is about handling StopIteration inside generatorn. So the proposal is if inside your generator a StopIterations is about to buble out, it will be replaced with RuntimeError and this will cause error when you call next() on your generator.
This may happen if you have iterator some_iterator and you call unguarded next(some_iterator). when it is exhausted it will raise StopIteration error. So, you should not allow this to happen and you need to handle it inside your generator. This is exactly the opposite to raisng it your self. There is even an example that says wrong as comment after raise StopIteration inside generator.

Your code in first post is exactly example how the PEP will affect older code. Note that you get RuntimeError, not StopIteration. That is the new behavior in accordance with the PEP - StopIteration error inside generator is replaced with RuntimeError. Thus you need to handle it inside your generator. But in this case you don't have to raise it artificially in the first place.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  can i raise an exception in a try clause? Skaperen 14 5,603 Dec-19-2019, 12:29 AM
Last Post: Skaperen
  Changing a traceback message without a 2nd raise Clunk_Head 1 1,947 Jul-14-2019, 12:45 AM
Last Post: Gribouillis
  During handling of the above exception, another exception occurred Skaperen 7 26,728 Dec-21-2018, 10:58 AM
Last Post: Gribouillis
  raise exception instead of return None Skaperen 4 4,051 Jul-19-2018, 01:27 AM
Last Post: ichabod801
  "Raise SMTPException("SMTP AUTH extension not supported by server.") NajeebUllah 3 7,919 Mar-16-2018, 09:45 PM
Last Post: nilamo
  receive from a generator, send to a generator Skaperen 9 5,423 Feb-05-2018, 06:26 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020