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

simon.bordeyne simon.bordeyne at gmail.com
Sun Jan 6 21:54:07 EST 2019

I knew that you can just chain try/except blocks, and it's how I do it now, but the example I provided wasn't very realistic.
Take for example the initialization of a class from a config file, config file which may or may not have certain keys in it. With many keys, it is very inconvenient to chain try/except blocks to handle every possible case. Having the continue keyword would prove useful to put several error prone lines of code into a single try block, and have the execution continue as normal at tge statement after the statement errored out 

Envoyé depuis mon smartphone Samsung Galaxy.
-------- Message d'origine --------De : Amber Yust <amber.yust at gmail.com> Date : 06/01/2019  09:07  (GMT+01:00) À : Simon <simon.bordeyne at gmail.com> Cc : python-ideas at python.org Objet : Re: [Python-ideas] Possible PEP regarding the use of the continue keyword in try/except blocks 
On Sat, Jan 5, 2019 at 4:39 PM Simon <simon.bordeyne at gmail.com> wrote: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"
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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190107/b066b3db/attachment-0001.html>

More information about the Python-ideas mailing list