run this action first statements finallystatements always run this code on the way out with this variantpython begins by running the statement block associated with the try header line as usual what happens next depends on whether an exception occurs during the try blockif an exception does not occur while the try block is runningpython continues on to run the finally blockand then continues execution past the try statement if an exception does occur during the try block' runpython still comes back and runs the finally blockbut it then propagates the exception up to previously entered try or the top-level default handlerthe program does not resume execution below the finally clause' try statement that isthe finally block is run even if an exception is raisedbut unlike an exceptthe 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 runsregardless of the exception behavior of the program in practiceit allows you to specify cleanup actions that always must occursuch as file closes and server disconnects where required note that the finally clause cannot be used in the same try statement as except and else in python and earlierso the try/finally is best thought of as distinct statement form if you are using an older release in python and laterhoweverfinally can appear in the same statement as except and elseso today there is really single try statement with many optional clauses (more about this shortlywhichever version you usethoughthe finally clause still serves the same purpose--to specify "cleanupactions that must always be runregardless of any exceptions as we'll also see later in this as of python and the new with statement and its context managers provide an object-based way to do similar work for exit actions unlike finallythis new statement also supports entry actionsbut it is limited in scope to objects that implement the context manager protocol it leverages examplecoding termination actions with try/finally we saw some simple try/finally examples in the prior here' more realistic example that illustrates typical role for this statementclass myerror(exception)pass def stuff(file)raise myerror(file open('data'' 'open an output file (this can fail toothe try/finally statement