代码点读机 / Python / finally

finally Python · 关键字

异常处理里的“收尾”块:不管 try 里出不出错,最后都会执行。

怎么用

finally:

小例子

try:
    print('干活')
finally:
    print('收尾')

运行结果

干活
收尾

官方文档:https://docs.python.org/zh-cn/3/tutorial/errors.html#tut-cleanup

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条