Python finally 关键字
-
定义和用法
finally关键字在try......except中使用。它定义了一个代码块,当try ... except ... else块结束时,该代码块将运行。finally无论try块是否引发错误,finally块都将被执行。这对于关闭对象和清理资源非常有用。 -
实例
无论try块是否引发错误,都将始终执行finally块:
尝试一下try: x > 3 except: print("Something went wrong") else: print("Nothing went wrong") finally: print("The try...except block is finished")
-
相关页面
Python 教程:Python try......except