On Tue, Jul 30, 2019 at 6:04 PM Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
Something I don't like about these kinds of proposals is that the except clause is far removed from the code that it covers, hurting readability. By the time you get to the end of a big for-loop or with-statement and see an "except", it's easy to forget that it isn't attached to an ordinary try-statement.
I would be happier if there were some way to get the except clause at the top instead of the bottom, but it's hard to think of a nice way to do that.
try with open('some_file') as f: except FileNotFoundError: do_something() else: do_something_else(f) was the best I could come up with, and I hate the `except` being indented (but I think a non-indented line right after a : is probably a non-starter). I do think even a clumsy solution is vaguely better than "just make your try: block encompass both the open() and then everything you do with the file after that".