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

前端 未结 14 1967
感情败类
感情败类 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 17:43

    In your first example, what happens if run_code1() raises an exception that is not TypeError? ... other_code() will not be executed.

    Compare that with the finally: version: other_code() is guaranteed to be executed regardless of any exception being raised.

提交回复
热议问题