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

Chris Barker chris.barker at noaa.gov
Sun Jan 6 17:29:35 EST 2019


There is already a much simpler way of doing this:

>     try:
>         i = int("string")
>     except ValueError as e:
>         print(e)
>     print("continued on")
>     j = int(9.0)
>
> The point of the 'try' block is to encapsulate the code you want to *stop*
> executing if an exception is raised. If you want code to be run regardless
> of whether an exception is raised, move it past the try-except.
>

To be fair, I suspect the issue was there were two calls to int() there
that might raise a ValueError, and the OP wanted to catch them with one
except, so you would need to do somethign like:

try:
    i = int("string")
except ValueError as e:
    print(e)
print("continued on")
try:
    j = int(9.0)
except ValueError as e:
    print(e)

Which can seem a bit verbose, but in fact, there are a number of ways one
might want to proceed with/without an error, and the current except,
finally, else options cover them all in a clearly defined way.

-CHB










>
> ~Amber
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190106/9fbd59a0/attachment.html>


More information about the Python-ideas mailing list