在python中打印异常的正确方法是什么?

时间:2018-12-20 10:23:47

标签: python exception format

至少有5种方法可以在python中打印异常信息。

except Exception as e:
     print(e)
     print(str(e))
     print(e.what())
     print('{}'.format(e))
     print(pformat(e))

正确的方法是什么,什么是pythonic?

2 个答案:

答案 0 :(得分:1)

在捕获异常后使用回溯:

import traceback
traceback.print_exc()

答案 1 :(得分:-1)

except Exception as e:
     print(e)

使用下面的链接获取更多示例 https://www.pythonforbeginners.com/error-handling/exception-handling-in-python