The official spec is
If finally is present, it specifies a ‘cleanup’ handler. The try clause is executed, including any except and else clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The finally clause is executed. If there is a saved exception it is re-raised at the end of the finally clause. If the finally clause raises another exception, the saved exception is set as the context of the new exception. If the finally clause executes a return or break statement, the saved exception is discarded:
So what happens in the fourth iteration of your loop is number
is set to "a"
and when you try to convert it to int
an exception is raised. Since there is no matching except
in the inner try
block the exception is saved, the finally
block is executed which gives the fourth ok
output and then the saved exception is reraised and caught by the outer try
block.