How to print an exception?

我怕爱的太早我们不能终老 提交于 2021-02-19 07:10:05

问题


How to print python exception?

Example:

try:
    action()
except:
    print "Unexpected error:", sys.exc_info()[0]

Prints:

Unexpected error: <type 'exceptions.TypeError'>

It does not have much information for me.


回答1:


Use traceback module:

try:
    action()
except:
    import traceback
    traceback.print_exc()



回答2:


You can print the exception which occurred too.

try:
    action()
except exception as ex:
    print("Exception: " + str(ex))


来源:https://stackoverflow.com/questions/45131565/how-to-print-an-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!