wilsonyin123/geekbangpython

try ... except... finally 问题!

Opened this issue · 1 comments

image

如图,还是会报NameError

老师这个例子应该是有问题的,他想表达的应该是
try:
f1 = open('name.txt')
i = 1 / 0
except Exception as e:
print(e)
finally:
f1.close()

你遇上这个问题应该是和java关闭流一样,一般finally会进行一个判空再进行关闭