Getting the exception value in Python

后端 未结 7 1132
一个人的身影
一个人的身影 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:32

    If you don't know the type/origin of the error, you can try:

    import sys
    try:
        doSomethingWrongHere()
    except:
        print('Error: {}'.format(sys.exc_info()[0]))
    

    But be aware, you'll get pep8 warning:

    [W] PEP 8 (E722): do not use bare except
    

提交回复
热议问题