如何正确获取 Python 中的异常消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33239308/
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
How to get exception message in Python properly
提问by FrozenHeart
What is the best way to get exceptions' messages from components of standard library in Python?
从 Python 标准库的组件中获取异常消息的最佳方法是什么?
I noticed that in some cases you can get it via message
field like this:
我注意到在某些情况下你可以通过这样的message
字段获得它:
try:
pass
except Exception as ex:
print(ex.message)
but in some cases (for example, in case of socket errors) you have to do something like this:
但在某些情况下(例如,在套接字错误的情况下)您必须执行以下操作:
try:
pass
except socket.error as ex:
print(ex)
I wondered is there any standard way to cover most of these situations?
我想知道是否有任何标准方法可以涵盖大多数这些情况?
采纳答案by ArtOfWarfare
If you look at the documentation for the built-in errors, you'll see that most Exception
classes assign their first argument as a message
attribute. Not all of them do though.
如果您查看内置错误的文档,您会看到大多数Exception
类将它们的第一个参数分配为message
属性。但并非所有人都这样做。
Notably,EnvironmentError
(with subclasses IOError
and OSError
) has a first argument of errno
, second of strerror
. There is no message
... strerror
is roughly analogous to what would normally be a message
.
值得注意的是,EnvironmentError
(带有子类IOError
和OSError
)的第一个参数是errno
,第二个参数是strerror
。没有message
...strerror
大致类似于通常的message
.
More generally, subclasses of Exception
can do whatever they want. They may or may not have a message
attribute. Future built-in Exception
s may not have a message
attribute. Any Exception
subclass imported from third-party libraries or user code may not have a message
attribute.
更一般地说, 的子类Exception
可以为所欲为。他们可能有也可能没有message
属性。未来的内置Exception
s 可能没有message
属性。Exception
从第三方库或用户代码导入的任何子类可能没有message
属性。
I think the proper way of handling this is to identify the specific Exception
subclasses you want to catch, and then catch only those instead of everything with an except Exception
, then utilize whatever attributes that specific subclass defines however you want.
我认为处理这个问题的正确方法是识别Exception
您想要捕获的特定子类,然后只捕获那些而不是所有带有 的except Exception
,然后利用特定子类定义的任何属性。
If you must print
something, I think that printing the caught Exception
itself is most likely to do what you want, whether it has a message
attribute or not.
如果您必须print
做某事,我认为打印捕获Exception
本身最有可能做您想做的事,无论它是否具有message
属性。
You could also check for the message attribute if you wanted, like this, but I wouldn't really suggest it as it just seems messy:
如果你愿意,你也可以检查 message 属性,像这样,但我不会真正建议它,因为它看起来很乱:
try:
pass
except Exception as e:
# Just print(e) is cleaner and more likely what you want,
# but if you insist on printing message specifically whenever possible...
if hasattr(e, 'message'):
print(e.message)
else:
print(e)
回答by Tosin Mash
To improve on the answer provided by @artofwarfare, here is what I consider a neater way to check for the message
attribute and print it or print the Exception
object as a fallback.
为了改进@artofwarfare提供的答案,我认为这是一种更简洁的方法来检查message
属性并打印它或打印Exception
对象作为后备。
try:
pass
except Exception as e:
print getattr(e, 'message', repr(e))
The call to repr
is optional, but I find it necessary in some use cases.
调用repr
是可选的,但我发现在某些用例中是必要的。
Update #1:
更新 #1:
Following the comment by @MadPhysicist, here's a proof of why the call to repr
might be necessary. Try running the following code in your interpreter:
在@MadPhysicist发表评论之后,这里证明了为什么repr
可能需要调用。尝试在解释器中运行以下代码:
try:
raise Exception
except Exception as e:
print(getattr(e, 'message', repr(e)))
print(getattr(e, 'message', str(e)))
Update #2:
更新#2:
Here is a demo with specifics for Python 2.7 and 3.5: https://gist.github.com/takwas/3b7a6edddef783f2abddffda1439f533
这是一个包含 Python 2.7 和 3.5 细节的演示:https: //gist.github.com/takwas/3b7a6edddef783f2abddffda1439f533
回答by Bill Z
I had the same problem. I think the best solution is to use log.exception, which will automatically print out stack trace and error message, such as:
我有同样的问题。我认为最好的解决方案是使用 log.exception,它会自动打印出堆栈跟踪和错误消息,例如:
try:
pass
log.info('Success')
except:
log.exception('Failed')