I want to catch all the exceptions but someone, eg KeyboardInterrupt,
the following is part of my code:
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)
It does not work.
If you are happy with also not catching SystemExit
and StopIteration
, just do
except Exception:
because that only catches "higher-level" exceptions. Still, this is considered bad practice. Always be specific in catching exceptions.
Catch and re-raise it before the general case
try:
#stuff
except KeyboardInterrupt:
raise #rethrow to a higher handler
except:
#everything else
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.