The finally block is used to define clean-up actions. Why is the finally block needed? Why can't the clean up actions be put after the try/except/else block? This works in some cases, but if there is a return, break, or continue, or an unhandled exception inside the try, except, or else clauses, that code will never be executed. The finally block executes even in these conditions.
try: print 'Inside try' raise Exception finally: print 'Inside finally' print 'Never get here'
Inside try Inside finally Traceback (most recent call last): File "tmp.py", line 13, in
raise Exception Exception