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@gmail.com>
Date : 06/01/2019 09:07 (GMT+01:00)
À : Simon <simon.bordeyne@gmail.com>
Cc : python-ideas@python.org
Objet : Re: [Python-ideas] Possible PEP regarding the use of the continue keyword in try/except blocks
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.
~Amber