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.ja va2 s .c o 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] # ww w . j a v a2 s . c o m x = 'test' def after(): try: fetcher(x, 4) finally: print('after fetch') print('after try?') after()