gpt4 book ai didi

python - 未捕获 TypeError 异常

转载 作者:行者123 更新时间:2023-11-28 22:23:31 26 4
gpt4 key购买 nike

为什么这个 TypeError 没有被异常捕获?

>>> op=None
>>> try:
... val = op['one']
... except KeyError, TypeError:
... val = "one"
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: 'NoneType' object has no attribute '__getitem__'
>>>

最佳答案

您必须将异常(exception)列表括在括号中:

except (KeyError, TypeError):
# ^ ^
val = "one"

关于python - 未捕获 TypeError 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46939163/

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