0

Which of these is easier to read?

An empty except block:

try:
    foo = do_foo()
except FooError:
    pass  # see below
else:
    return do_bar(foo)
# Recover from the FooError (long, complicated)

Or this:

try:
    foo = do_foo()
except FooError:
    # Recover from the FooError (long, complicated)
else:
    return do_bar(foo)

Note that the recovery is significantly more complex than the one-line else block. It may itself involve indented blocks or other complications. The first option has the advantage of eliminating a level of indentation. But the second option is probably closer to what the average developer expects to see. I'm uncertain which of these concerns is more important for readability.

3
  • They're not equivalent. In the first code snippet you'll "recover" unconditionally, even when the exception isn't thrown. If you're concerned about the size of the recovery code, you can just put it in a function. – Doval Jan 28 '15 at 21:51
  • @Doval: Nope, the else returns. The snippets are exactly equivalent. – Kevin Jan 28 '15 at 21:51
  • My apologies, you're right. It's been a while since I touch Python. – Doval Jan 28 '15 at 21:53
3

I think you are accurate about the trade-off involved - neither is ideal. Out of those two, I personally prefer the first one since the control flow is easier to follow.

That said, I'd suggest considering a third option that you didn't mention: factor out some or all of the recovery logic into a separate function. That keeps the control flow simple and avoids having a big block of recovery code in the except clause.

3

Explicit is better than implicit.

If you are handling an exception, handle it. Right there, right then.

1
  • 1
    Exactly, and if recovery is too long refactor it into a suitably named function. – mkalkov Jan 28 '15 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.