Printing out actual error message for ValueError

后端 未结 2 1579
慢半拍i
慢半拍i 2021-02-06 21:06

How can I actually print out the ValueError\'s message after I catch it?

If I type except ValueError, err: into my code instead of except ValueError a

相关标签:
2条回答
  • 2021-02-06 21:21
    try:
        ...
    except ValueError as e:
        print(e)
    
    0 讨论(0)
  • 2021-02-06 21:29

    Python 3 requires casting the exception to string before printing:

    try:
        ...
    except ValueError as error:
        print(str(error))
    
    0 讨论(0)
提交回复
热议问题