Why do we need the “finally” clause in Python?

前端 未结 14 1954
感情败类
感情败类 2020-11-28 16:58

I am not sure why we need finally in try...except...finally statements. In my opinion, this code block

try:
    run_code1()
except          


        
14条回答
  •  温柔的废话
    2020-11-28 18:02

    finally is for defining "clean up actions". The finally clause is executed in any event before leaving the try statement, whether an exception (even if you do not handle it) has occurred or not.

    I second @Byers's example.

提交回复
热议问题