gpt4 book ai didi

python - 为什么不显示 AssertionError?

转载 作者:行者123 更新时间:2023-11-28 19:39:53 26 4
gpt4 key购买 nike

作为实验,我 try catch 失败的断言。

try: assert 1==2
except Exception as e: print e

为什么没有显示?

最佳答案

>>> try: assert 1==2
... except Exception as e: print type(e)
...
<type 'exceptions.AssertionError'>

>>> try: assert 1==2, "They Are Not Equal!!"
... except Exception as e: print e
...
They Are Not Equal!!

至于为什么:当你调用 print 时它正在调用异常的 __str__ 方法......因为你没有在那里放任何文本,你的文本是空字符串...这是打印的内容。

关于python - 为什么不显示 AssertionError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17372957/

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