Why does the function not throw an exception? Apparently he is not caught.
def f(): try: raise Exception finally: return "ok" print(f()) # ok
This is clearly explained in the documentation :
If an exception occurs in any of the sentences and is not processed, the exception is temporarily saved. The offer is being executed finally. [..] If finallythe statement is executed by returnor break, the stored exception is discarded
finally
return
break
From docs :
, try.
@deceze
finally , , .
:
>>> try: ... raise Exception ... finally: ... print('yes') ... yes Traceback (most recent call last): File "<stdin>", line 2, in <module> Exception
, , "" , .
finally , try, . [...] finally " " , try break, continue return statement.
, finally, . :
def f(): try: return 'OK' finally: return 'Finally' f() # returns 'Finally'
finally , try, . try except ( except else), finally .
try
except
else
, finally, finally, .