为什么return的返回值最后被打印呢?
def test2():
try:
1 / 0
except Exception as e:
print('111')
return e
finally:
print('finally')
result = test2()
print(result)
运行结果:
111
finally
division by zero
result = test2()# 程序从这开始执行,执行过程为调用test2函数,再把返回值赋值给result
print(result) # 打印result
运行test2函数时,打印111,再返回 e,再打印finally,也就是返回了"division by zero"
把"division by zero"赋值给result
继续运行print(result)
print('111') 和print('finally')是在result = test2()过程中被打印的
print(result)在result = test2()的下方,所以最后打印 逻辑是这样的:
先尝试1/0报错
然后错误存到e中,打印111
然后返回e到result中
然后运行finally中的print
最后再运行print(result)
页:
[1]