Using "try"+"finally" without "except" never generates any error [duplicate] Using "try"+"finally" without "except" never generates any error [duplicate] python-3.x python-3.x

Using "try"+"finally" without "except" never generates any error [duplicate]


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 it's 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.