Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use Python's "else" clause in try/except block? [duplicate]

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?

like image 955
odigity Avatar asked Jan 29 '13 18:01

odigity


People also ask

What is the purpose of the else clause in a try-except construct?

The try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no error. The finally block lets you execute code, regardless of the result of the try- and except blocks.

Under what condition is the else clause in a try-except block executed?

Else Clause The code enters the else block only if the try clause does not raise an exception. Example: Else block will execute only when no exception occurs.

Do you need else in TRY-except Python?

The use of the else clause is better than adding additional code to the try clause because it avoids accidentally catching an exception that wasn't raised by the code being protected by the try ... except statement.

Why does Python use except instead of catch?

The reason to use try/except is when you have a code block to execute that will sometimes run correctly and sometimes not, depending on conditions you can't foresee at the time you're writing the code.


2 Answers

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
like image 116
Izkata Avatar answered Sep 21 '22 05:09

Izkata


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]"

like image 44
dmg Avatar answered Sep 22 '22 05:09

dmg