在 Python 中捕获 KeyError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16154032/
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
Catch KeyError in Python
提问by spizzak
If I run the code:
如果我运行代码:
connection = manager.connect("I2Cx")
The program crashes and reports a KeyError because I2Cx doesn't exist (it should be I2C).
程序崩溃并报告 KeyError 因为 I2Cx 不存在(它应该是 I2C)。
But if I do:
但如果我这样做:
try:
connection = manager.connect("I2Cx")
except Exception, e:
print e
It doesn't print anything for e. I would like to be able to print the exception that was thrown. If I try the same thing with a divide by zero operation it is caught and reported properly in both cases. What am I missing here?
它不会为 e 打印任何内容。我希望能够打印抛出的异常。如果我用除以零操作尝试同样的事情,它会在两种情况下被捕获并正确报告。我在这里缺少什么?
采纳答案by Aya
If it's raising a KeyError with no message, then it won't print anything. If you do...
如果它在没有消息的情况下引发 KeyError ,那么它不会打印任何内容。如果你这样做...
try:
connection = manager.connect("I2Cx")
except Exception as e:
print repr(e)
...you'll at least get the exception class name.
...你至少会得到异常类名。
A better alternative is to use multiple exceptblocks, and only 'catch' the exceptions you intend to handle...
更好的选择是使用多个except块,并且只“捕获”您打算处理的异常......
try:
connection = manager.connect("I2Cx")
except KeyError as e:
print 'I got a KeyError - reason "%s"' % str(e)
except IndexError as e:
print 'I got an IndexError - reason "%s"' % str(e)
There are valid reasons to catch all exceptions, but you should almost always re-raise them if you do...
捕获所有异常是有正当理由的,但是如果你这样做,你几乎总是应该重新引发它们......
try:
connection = manager.connect("I2Cx")
except KeyError as e:
print 'I got a KeyError - reason "%s"' % str(e)
except:
print 'I got another exception, but I should re-raise'
raise
...because you probably don't want to handle KeyboardInterruptif the user presses CTRL-C, nor SystemExitif the try-block calls sys.exit().
......因为你可能不想来处理KeyboardInterrupt,如果用户按下CTRL-C,也不SystemExit若try-阻塞调用sys.exit()。
回答by RichieHindle
You should consult the documentation of whatever library is throwing the exception, to see how to get an error message out of its exceptions.
您应该查阅引发异常的任何库的文档,以了解如何从异常中获取错误消息。
Alternatively, a good way to debug this kind of thing is to say:
或者,调试此类事情的一个好方法是说:
except Exception, e:
print dir(e)
to see what properties ehas - you'll probably find it has a messageproperty or similar.
看看有什么属性e- 你可能会发现它有一个message属性或类似的。
回答by Joran Beasley
I dont think python has a catch :)
我不认为 python 有问题:)
try:
connection = manager.connect("I2Cx")
except Exception, e:
print e
回答by kenorb
回答by Amrit
If you don't want to handle error just NoneTypeand use get()e.g.:
如果您不想仅处理错误NoneType并使用get()例如:
manager.connect.get("")
回答by Mic
Try print(e.message) this should be able to print your exception.
尝试 print(e.message) 这应该能够打印您的异常。
try:
connection = manager.connect("I2Cx")
except Exception, e:
print(e.message)
回答by ajpieri
I am using Python 3.6 and using a comma between Exception and e does not work. I need to use the following syntax (just for anyone wondering)
我正在使用 Python 3.6 并且在 Exception 和 e 之间使用逗号不起作用。我需要使用以下语法(仅适用于任何想知道的人)
try:
connection = manager.connect("I2Cx")
except KeyError as e:
print(e.message)

