defining clean-up actions the try statement has another optional clause which is intended to define clean-up actions that must be executed under all circumstances for exampletryraise keyboardinterrupt finallyprint('goodbyeworld!'goodbyeworldkeyboardinterrupt traceback (most recent call last)file ""line in finally clause is always executed before leaving the try statementwhether an exception has occurred or not when an exception has occurred in the try clause and has not been handled by an except clause (or it has occurred in an except or else clause)it is re-raised after the finally clause has been executed the finally clause is also executed "on the way outwhen any other clause of the try statement is left via breakcontinue or return statement more complicated exampledef divide(xy)tryresult except zerodivisionerrorprint("division by zero!"elseprint("result is"resultfinallyprint("executing finally clause"divide( result is executing finally clause divide( division by zeroexecuting finally clause divide(" "" "executing finally clause traceback (most recent call last)file ""line in file ""line in divide typeerrorunsupported operand type(sfor /'strand 'stras you can seethe finally clause is executed in any event the typeerror raised by dividing two strings is not handled by the except clause and therefore re-raised after the finally clause has been executed in real world applicationsthe finally clause is useful for releasing external resources (such as files or network connections)regardless of whether the use of the resource was successful predefined clean-up actions some objects define standard clean-up actions to be undertaken when the object is no longer neededregardless of whether or not the operation using the object succeeded or failed look at the following examplewhich tries to open file and print its contents to the screen errors and exceptions