In the previous articles, we covered the try and except clauses. In this article, we will start out by looking at the else clause.
Python Exceptions: The Else Clause
Although the purpose of the else clause may not be readily apparent, it is necessary because without it, there is no way to tell whether the flow of control has proceeded past a try statement because no exception was raised, or because an exception occurred and was handled – at least not without setting and checking Boolean flags. Much like the way else clauses in loops make the exit cause more apparent, the else clause provides syntax in a try that makes what has happened obvious and unambiguous. For example:
try:
# Run some code
except SomeError:
# Handle the exception
else
# No exception has occurred
You can do something similar by moving the else code into the try block. This can lead, however, to incorrect exception classifications. If the “no exception occurred” action triggers a SomeError, it will register as a failure of the try block and erroneously trigger the exception handler below the try. By using an explicit else clause instead, you will make the logic more obvious and guarantee that except handlers will run only for real failres in the code you are wrapping in a try, not for failures in the else case’s action.
The other variant of the try statement is a specialization that has to do with finalization actions. If a finally clause is included in a try, Python will always run its block of statements “on the way out” of the try statement, whether an exception occured while the try block was running or not. Its general form is:
try:
finally:
With this variant, Python begins by running the statement block associated with the try header line. What happens next depends on whether an exception occurs during the try block. If no exception occurs while the try block is running, Python jumps back to run the finally block and then continues execution past below the try statement. If on the other hand an exception does occur during the try block’s run, Python still comes back and runs the finally block, but it then propagates the exception up to a higher try or the top-level default handler; the program does not resume execution below the try statement. That is, the finally block is run even if an exception is raised, but unlike an except, the finally does not terminate the exception – it continues being raised after the finally block runs.
The try/finally form is useful when you want to be completely sure that an action will happen after some code runs, regardless of the exception behavior of the program. In practice, it allows you to specify cleanup actions that always must occur (such as file closes).
Note that the finally clase cannot be used in the same try statement as except and else in Python 2.4 and earlier, so the try/finally blocks are best thought of as a distinct statement form if you are using an older version of Python. In Python 2.5 and later, however, finally can appear in the same statement as except and else, so today there is essentially a single try statement with many optional clauses. Whichever version you use, though, the finally clause still serves the same purpose: to specify cleanup actions that must always be run, regardless of any exceptions.
Recent Comments