[英]How to catch all the exceptions but someone in python?
我想捕获所有异常,但有人, eg KeyboardInterrupt,
以下是我的代码的一部分:
try:
num = 70
while 1:
print 'hello %d' % num
sleep(0.1)
num += 1
a = 1/(num - 80)
except not KeyboardInterrupt:
print 'ctrl c'
save(num)
这是行不通的。
如果您也对不赶上SystemExit
和StopIteration
感到满意,那就去做吧。
except Exception:
因为这只会捕获“更高级别”的异常。 尽管如此,这仍被认为是不好的做法。 在捕获异常时始终要具体。
在一般情况下抓住并重新加注
try:
#stuff
except KeyboardInterrupt:
raise #rethrow to a higher handler
except:
#everything else
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.