Python 打印 ValueError 的实际错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4097461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing out actual error message for ValueError
提问by wrongusername
How can I actually print out the ValueError's message after I catch it?
捕捉到 ValueError 的消息后,如何实际打印出它?
If I type except ValueError, err:into my code instead of except ValueError as err:, I get the error SyntaxError: invalid syntax.
如果我输入except ValueError, err:我的代码而不是except ValueError as err:,我会收到错误SyntaxError: invalid syntax。
采纳答案by snapshoe
try:
...
except ValueError as e:
print(e)
回答by Bengt
Python 3 requires casting the exception to string before printing:
Python 3 需要在打印前将异常转换为字符串:
try:
...
except ValueError as error:
print(str(error))

