How to determine if an exception was raised once you're in the finally block?

后端 未结 6 1766
一整个雨季
一整个雨季 2020-12-14 06:06

Is it possible to tell if there was an exception once you\'re in the finally clause? Something like:

try:
    funky code
finally:
    if ???:
          


        
6条回答
  •  执念已碎
    2020-12-14 06:54

    If exception happened --> Put this logic in the exception block(s).
    If exception did not happen --> Put this logic in the try block after the point in code where the exception can occur.

    Finally blocks should be reserved for "cleanup actions," according to the Python language reference. When finally is specified the interpreter proceeds in the except case as follows: Exception is saved, then the finally block is executed first, then lastly the Exception is raised.

提交回复
热议问题