try statements can include finally blocks.
The finally specifies termination actions that always execute regardless of whether an exception occurs in the try block or not;
def fetcher(obj, index): return obj[index] # from w w w . java 2s. co m x = 'test' try: fetcher(x, 3) finally: # Termination actions print('after fetch')
Here, if the try block finishes without an exception, the finally block will run, and the program will resume after the entire try.
def fetcher(obj, index): return obj[index] # w ww . j a v a 2s .c om x = 'test' def after(): try: fetcher(x, 4) finally: print('after fetch') print('after try?') after()