There are two differences:
- The relative order in which the except and finally blocks execute differs. In version 1, the finally executes before the except. In version 2 the excecution order is reversed.
- In version 1, if the finally block raises, then it will be handled by the except block. In version 2, if the finally block raises, then it will be handled by the next containing exception handler, i.e. outside this code.
Usually you aren't concerned about finally blocks that raise. You simply don't expect that to happen and if it does, something is probably very broken.
So the difference that counts is whether the finally runs before the exception handler, or vice versa. Sometimes it doesn't matter, but it often does make a difference.
do something
part or earlier? - mjnfinally
blocks andexcept
blocks are both introduced with the sametry
keyword because they're otherwise orthogonal concepts. - Rob Kennedy