6

Possible Duplicate:
Python try-else

I'm not seeing the benefit of it, at least based on the example I just read in Dive Into Python:

try:
    from EasyDialogs import AskPassword
except ImportError:
    getpass = default_getpass
else:
    getpass = AskPassword

(http://www.diveintopython.net/file_handling/index.html)

Why couldn't you achieve the same effect with the shorter/simpler:

try:
    from EasyDialogs import AskPassword
    getpass = AskPassword
except ImportError:
    getpass = default_getpass

What am I missing?

10
  • 1
    What if the line getpass = AskPassword could raise an ImportError itself?
    – mgilson
    Jan 29, 2013 at 18:55
  • 2
    Dup? Python try-else
    – Izkata
    Jan 29, 2013 at 18:57
  • 3
    The problem with this is that you end up using your exceptions for controlflow instead of for error handling. Jan 29, 2013 at 18:57
  • @Izkata I think you can hit the close button on this one without asking for second opinions :P... Jan 29, 2013 at 18:59
  • 2
    In this case the else clause verbose because you can do from EasyDialogs import AskPassword as getpass Jan 29, 2013 at 19:06

2 Answers 2

6

There isn't an advantage in the example, except possibly for style. It's generally a good idea to keep code that can cause exceptions near the code that deals with them. For example, compare these:

try:
    from EasyDialogs import AskPassword
    # 20 other lines
    getpass = AskPassword
except ImportError:
    getpass = default_getpass

and

try:
    from EasyDialogs import AskPassword
except ImportError:
    getpass = default_getpass
else:
    # 20 other lines
    getpass = AskPassword

The second one is good when the except can't return early, or re-throw the exception. If possible, I would have written:

try:
    from EasyDialogs import AskPassword
except ImportError:
    getpass = default_getpass
    return False // or throw Exception('something more descriptive')

# 20 other lines
getpass = AskPassword
1
  • It's still a dupe I think, but as for code style I'll vote up. Better add it to the duplicates answers though. Jan 29, 2013 at 19:10
2

I personally find it clearer in some situations. Naturally the greater deal of code should be ran when an exception does not occur. So in a way you are saying:

try:
    this_very_dangerous_call()
except ValueError:
    # if it breaks
    handle_value_error()
else:
    continue_with_my_code()
    # more code

Thus you are visually separating the exception handling code from the rest of the code. It's like saying: "Try this, if it breaks do something, if it doesn't [insert long explanation here]"

3
  • Problem is I don't like that ordering. I think the intended/optimistic code path should be prominent, and exception handling afterwards. This pattern puts it in the middle, between the two halves of the primary paths. Doesn't sit well with me.
    – odigity
    Jan 29, 2013 at 19:13
  • Well it can be used to distinguish the parts of the code that can and can't throw an exception. But this is also in a way similar to the situation where you have a function which is something like "if a then b else c" and the question being whether the "common case" (which is a lot longer and has more logic in it) should be the "then" block or the "else" block?
    – dmg
    Jan 29, 2013 at 19:18
  • Also have a look at stackoverflow.com/questions/855759/python-try-else
    – dmg
    Jan 29, 2013 at 19:27

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