[Python-ideas] Possible PEP regarding the use of the continue keyword in try/except blocks
Robert Collins
robertc at robertcollins.net
Sun Jan 6 01:15:13 EST 2019
On Sun., 6 Jan. 2019, 13:39 Simon <simon.bordeyne at gmail.com 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 :
>
In terms of implementation, I think continue would be problematic
while true:
try:
x = foo()
return x
except:
continue
is already valid code. You'd need some way of disambiguating, either a
keyword or parameter to continue. Both of which would require a very big
benefit for us to do, given the ecosystem impact that such things have.
> 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"
>
Exception handling is not internally line orientated, so this proposed
resume functionality doesn't map exactly. But if the following in the same
way as what you envision:
def handle(f, *args):
try:
return f(*args)
except ValueError as e:
print(e)
i = handle( int, "string")
handle(print, "continued on")
j = handle(int, 9.0)
Then I have to say I'm not sure what you are trying to solve. Is it the
verbosity? Is it the flow control?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190106/b947e398/attachment.html>
More information about the Python-ideas
mailing list