在python中处理特定的异常类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4329453/
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
Handle specific exception type in python
提问by Falmarri
I have some code that handles an exception, and I want to do something specific only if it's a specific exception, and only in debug mode. So for example:
我有一些处理异常的代码,我只想在它是特定异常时做一些特定的事情,并且只在调试模式下。例如:
try:
stuff()
except Exception as e:
if _debug and e is KeyboardInterrupt:
sys.exit()
logging.exception("Normal handling")
As such, I don't want to just add a:
因此,我不想只添加一个:
except KeyboardInterrupt:
sys.exit()
because I'm trying to keep the difference in this debug code minimal
因为我试图将这个调试代码的差异保持在最小
采纳答案by mipadi
Well, really, you probably should keep the handler for KeyboardInterruptseparated. Why would you only want to handle keyboard interrupts in debug mode, but swallow them otherwise?
好吧,真的,您可能应该将处理程序KeyboardInterrupt分开。为什么你只想在调试模式下处理键盘中断,否则吞下它们?
That said, you can use isinstanceto check the type of an object:
也就是说,您可以使用isinstance来检查对象的类型:
try:
stuff()
except Exception as e:
if _debug and isinstance(e, KeyboardInterrupt):
sys.exit()
logger.exception("Normal handling")
回答by S.Lott
This is pretty much the way it's done.
这几乎就是这样做的方式。
try:
stuff()
except KeyboardInterrupt:
if _debug:
sys.exit()
logging.exception("Normal handling")
except Exception as e:
logging.exception("Normal handling")
There's minimal repetition. Not zero, however, but minimal.
有最少的重复。然而,不是零,而是最小的。
If the "normal handling" is more than one line of code, you can define a function to avoid repetition of the two lines of code.
如果“正常处理”不止一行代码,可以定义一个函数来避免两行代码的重复。
回答by Velociraptors
What's wrong with
怎么了
try:
stuff()
except KeyboardInterrupt:
if _debug:
logging.exception("Debug handling")
sys.exit()
else:
logging.exception("Normal handling")
回答by moinudin
Either use the standard method mentioned in the other answers, or if you really want to test within the except block then you can use isinstance().
要么使用其他答案中提到的标准方法,要么如果您真的想在 except 块中进行测试,那么您可以使用isinstance()。
try:
stuff()
except Exception as e:
if _debug and isinstance(e, KeyboardInterrupt):
sys.exit()
logging.exception("Normal handling")
回答by Paulo Scardine
You should let KeyboardInterrupt bubble all the way up and trap it at the highest level.
您应该让 KeyboardInterrupt 一直冒泡并将其捕获在最高级别。
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
sys.exit()
except:
pass
def main():
try:
stuff()
except Exception as e:
logging.exception("Normal handling")
if _debug:
raise e
回答by Lex
try:
stuff()
except KeyboardInterrupt:
if _debug:
sys.exit()
logging.exception("Normal handling")
except ValueError:
if _debug:
sys.exit()
logging.exception("Value error Normal handling")
else:
logging.info("One more message without exception")
回答by Chris Pfohl
You can name specific exceptions in Python:
您可以在 Python 中命名特定的异常:
try:
stuff()
except KeyboardInterrupt:
sys.exit()
except Exception:
normal_handling()

