python 3.x – Using try+finally without except never generates any error
python 3.x – Using try+finally without except never generates any error
You can find documented in the section about the try statement:
If the finally clause executes a return, break or continue statement, the saved exception is discarded.
Of course, the fact that its a documented behavior does not entirely explain the reasoning, so I offer this: a function can only exit in one of two ways, by returning a value or raising an exception. It can not do both.
Since the finally block is commonly used as a cleanup handler, it makes sense for the return to take priority here. You do have the chance to re-raise any exceptions within finally, simply by not using a return statement.