作为实验,我 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/