Getting the exception value in Python

后端 未结 7 944
一个人的身影
一个人的身影 2021-01-29 20:15

If I have that code:

try:
    some_method()
except Exception, e:

How can I get this Exception value (string representation I mean)?

7条回答
  •  伪装坚强ぢ
    2021-01-29 20:40

    Even though I realise this is an old question, I'd like to suggest using the traceback module to handle output of the exceptions.

    Use traceback.print_exc() to print the current exception to standard error, just like it would be printed if it remained uncaught, or traceback.format_exc() to get the same output as a string. You can pass various arguments to either of those functions if you want to limit the output, or redirect the printing to a file-like object.

提交回复
热议问题