Python Forum
raise exception within generator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
raise exception within generator
#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


Messages In This Thread
raise exception within generator - by bermudj - May-24-2020, 04:23 PM
RE: raise exception within generator - by buran - May-24-2020, 04:58 PM
RE: raise exception within generator - by bermudj - Jun-06-2020, 09:51 AM
RE: raise exception within generator - by buran - Jun-06-2020, 11:56 AM

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