例如:
无论try块是否引发错误,都将始终执行finally代码块:
try:
x > 3
except:
print("Something went wrong")
else:
print("Nothing went wrong")
finally:
print("The try...except block is finished")
1、定义和用法
finally
关键字在try...except块中使用。它定义了一个代码块,当try...except...else块结束时,该代码块将运行。
无论try块是否引发错误,都将执行finally
代码块。
关闭对象和清理资源会经常用到。