python中的perror等价函数

时间:2010-02-08 06:57:29

标签: python try-catch

我在python中使用try except block,而try块失败,如何打印有意义的错误消息。我正在寻找像C

中的perror()之类的东西

2 个答案:

答案 0 :(得分:6)

>>> try:
...     0/0
... except Exception,e:
...     print e.message
...
integer division or modulo by zero

或在Python 2.6及更高版本中,e.args,因为BaseException.message has been deprecated

>>> try:
...     0/0
... except Exception,e:
...     print e.args
...
('integer division or modulo by zero',)

答案 1 :(得分:-2)

try:
   pass
except Exception, err:
   print err