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


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,971 Dec-19-2019, 12:29 AM
Last Post: Skaperen
  Changing a traceback message without a 2nd raise Clunk_Head 1 2,036 Jul-14-2019, 12:45 AM
Last Post: Gribouillis
  During handling of the above exception, another exception occurred Skaperen 7 27,108 Dec-21-2018, 10:58 AM
Last Post: Gribouillis
  raise exception instead of return None Skaperen 4 4,232 Jul-19-2018, 01:27 AM
Last Post: ichabod801
  "Raise SMTPException("SMTP AUTH extension not supported by server.") NajeebUllah 3 8,120 Mar-16-2018, 09:45 PM
Last Post: nilamo
  receive from a generator, send to a generator Skaperen 9 5,693 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