[Python-ideas] with-except-finally blocks

Kale Kundert kale at thekunderts.net
Thu Apr 16 17:20:07 CEST 2015


On 04/16/2015 12:22 AM, Andrew Barnert wrote:
> Why does the finally come before the except? Also, it's still indented with the with, which means you seem to be defining with-except-finally in terms of with-finally, and not defining with-finally at all. Did you mean to have the finally after the except and dedented?

Oops, my bad.  I did mean to put the finally-block after the except-block:

try:
    with open('spam.txt') as file:
        print(file.read())
except IOError:
    print('No spam here...')
finally:
    print('done.')


More information about the Python-ideas mailing list