The Optional `else` in Python’s `try` Statement
This post discusses the
else clause in Python’s
try statement. Although this particular use of
else may not be as forgotten and controversial as its use in Python loops, a gentle reminder of how it works and when it’s useful may still be beneficial. Please note that this post only focuses on the
else clause of the
try statement and does not intend to cover the entire
try statement. For a pretty good initial explanation of how the entire
try statement works, refer to the official Python documentation on this topic.
A “normal” stripped down
try statement seen regularly looks something like this:
# some code that might raise exception
# code to handle exception
# code to run regardless of exception
This is “normal” in the sense that to anyone coming from most other programming languages can understand/guess what it means:
- Some code will be tried to be executed.
- If an exception is raised, the `except` block is executed, if not, it isn’t.
- Regardless of an exception being raised or not, the code in the `finally` block will be executed.
The big difference between exception handling in Python and other languages is that in Python using exceptions for flow control is common and normal. In fact, in many cases, you must use exceptions for control-flow. This is where the
else clause comes in.
else clause, which has to be before the
finally clause (if there is one), is executed if and when control flows off the end of the
try clause. Currently, control “flows off the end” except in the case of an exception or the execution of a
break statement. So, for example:
... print("Try: I'll try not to raise an exception.")
... print("Except: You hopefully won't see me.")
... print("Else: No exceptions was raised.")
... print("Finally: I'm going to run anyway.")
Try: I'll try not to raise an exception.
Else: No exceptions was raised.
Finally: I'm going to run anyway.
As mentioned earlier, it is very common to use Python’s
try statement as to control the flow of our program. So, the
else clause is a very neat way of executing some code if an exception isn’t raised.
For instance, one way to check if a file exists is:
f = open('some-file.txt')
print("File doesn't exist :-(")
some-file.txt exists and opens successfully, then an exception will not be raised and the
else clause will be executed (
File exists! will be printed). Otherwise, the
else clause will not be executed (
File doesn’t exist :-( will be printed).
Also, without the
else clause, the only option to run additional code before finalisation (which is rare) would be the clumsy practice of adding the code to the
try clause. This is clumsy because it risks raising exceptions in code that wasn’t intended to be protected by the
In general, in Python, there is probably not a case where you have no choice but to use the
else clause in a
try statement. But, besides allowing you to control the flow of your program without much hassle, it will often make the code more elegant, readable and Pythonic as well.
In other news, if you’re interested in a curated list of must-watch Python videos, check out my py-must-watch list on Highlight.