I am not sure why we need
finally
try...except...finally
try:
run_code1()
except TypeError:
run_code2()
other_code()
finally
try:
run_code1()
except TypeError:
run_code2()
finally:
other_code()
It makes a difference if you return early:
try:
run_code1()
except TypeError:
run_code2()
return None # The finally block is run before the method returns
finally:
other_code()
Compare to this:
try:
run_code1()
except TypeError:
run_code2()
return None
other_code() # This doesn't get run if there's an exception.
Other situations that can cause differences:
run_code1()
but it's not a TypeError
.continue
and break
statements.