[Python-ideas] Possible PEP regarding the use of the continue keyword in try/except blocks

Richard Damon Richard at Damon-Family.org
Sat Jan 5 19:49:30 EST 2019


On 1/5/19 7:38 PM, Simon wrote:
>
> I was writing some python code earlier, and I noticed that in a code
> that looks somwhat like this one :
>
>     try:
>         i = int("string")
>         print("continued on")
>         j = int(9.0)
>     except ValueError as e:
>         print(e)
>
> >>> "invalid literal for int() with base 10: 'string'"
>
> this code will handle the exception, but the code in the try block
> will not continue.
>
> I propose to be able to use the continue keyword to continue the
> execution of the try block even when an error is handled. The above
> could then be changed to :
>
>
>     try:
>         i = int("string")
>         print("continued on")
>         j = int(9.0)
>     except ValueError as e:
>         print(e)
>         continue
>
> >>> "invalid literal for int() with base 10: 'string'"
> >>> "continued on"
>
How would you tell it where to continue? Why would it be the next
statement? If you want that then you just need to do it like:


try:

    i = int("string")

except ValueError as e:

    print(e)

print("continued on")

j = int(9.0)

i.e. the try block is the program segment that either executes
successful, or your exception routine recovers from the error and sets
things up to continue from there.

-- 
Richard Damon



More information about the Python-ideas mailing list