gpt4 book ai didi

如果最终返回值,则不会引发 python 异常

转载 作者:太空宇宙 更新时间:2023-11-03 11:39:10 26 4
gpt4 key购买 nike

<分区>

谁能解释为什么下面的例子没有引发Exception

def foo():
try:
0/0
except Exception:
print('in except')
raise
finally:
print('in finally')
return 'bar'

my_var = foo()
print(my_var)

这只是返回:

in except
in finally
bar

如果没有 return 'bar' 语句的相同代码抛出异常:

in except
in finally
Traceback (most recent call last):
File "test.py", line 10, in <module>
my_var = foo()
File "test.py", line 3, in foo
0/0
ZeroDivisionError: division by zero

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com