The statements in the else block are executed if execution falls off the bottom of the try - if there was no exception. Honestly, I've never found a need.
However, Handling Exceptions notes:
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.
So, if you have a method that could, for example, throw an IOError, and you want to catch exceptions it raises, but there's something else you want to do if the first operation succeeds, and you don't want to catch an IOError from that operation, you might write something like this:
try:
operation_that_can_throw_ioerror()
except IOError:
handle_the_exception_somehow()
else:
# we don't want to catch the IOError if it's raised
another_operation_that_can_throw_ioerror()
finally:
something_we_always_need_to_do()
If you just put another_operation_that_can_throw_ioerror() after operation_that_can_throw_ioerror, the except would catch the second call's errors. And if you put it after the whole try block, it'll always be run, and not until after the finally. The else lets you make sure
- the second operation's only run if there's no exception,
- it's run before the
finallyblock, and - any
IOErrors it raises aren't caught here
The statements in the else block are executed if execution falls off the bottom of the try - if there was no exception. Honestly, I've never found a need.
However, Handling Exceptions notes:
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.
So, if you have a method that could, for example, throw an IOError, and you want to catch exceptions it raises, but there's something else you want to do if the first operation succeeds, and you don't want to catch an IOError from that operation, you might write something like this:
try:
operation_that_can_throw_ioerror()
except IOError:
handle_the_exception_somehow()
else:
# we don't want to catch the IOError if it's raised
another_operation_that_can_throw_ioerror()
finally:
something_we_always_need_to_do()
If you just put another_operation_that_can_throw_ioerror() after operation_that_can_throw_ioerror, the except would catch the second call's errors. And if you put it after the whole try block, it'll always be run, and not until after the finally. The else lets you make sure
- the second operation's only run if there's no exception,
- it's run before the
finallyblock, and - any
IOErrors it raises aren't caught here
There is one big reason to use else - style and readability. 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
Note: Answer copied from recently-posted duplicate here, hence all this "AskPassword" stuff.
try except else
Why do we use try/except?
so what is the different here?
If you don't catch it, the program halts. If you use a try / except block, then you can program in what to do if the input is invalid. A normal choice would be to prompt the user that the input was invalid and try again.
Furthermore, it seems like all the try/except examples could be replicated with an if statements.
Yes at this point. But that won't always be the case. For example, you can't have an if statement that will tell if a http connection will work. Also, an if / else block does not gain you anything; it's just as long or longer as a try / except block, and slightly slower.
More important however is python's "duck typing" philosophy. In python, we don't waste time with tests; we blindly forge ahead and have the except block as a backup plan. In other words, if you expect a duck, and the object you get quacks, that's good enough. No need to test if it actually is a duck.
See python's definition's of duck-typing, EAFP and LBYL.
More on reddit.comI'm new to Try and Except clauses. I don't understand why this code doesn't work.
Try and Except... Is there a way to make except loop back to a specific point.
My understanding is that Else runs if Try succeeds without any exceptions. What are the uses for this where you couldn’t just put that code in the Try statement?