sad*_*ave 5 python systemexit infinite-loop keyboardinterrupt try-except
按下Ctrl + C时,我的while循环不会退出.它似乎忽略了我的KeyboardInterrupt异常.循环部分如下所示:
while True:
try:
if subprocess_cnt <= max_subprocess:
try:
notifier.process_events()
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
notifier.stop()
break
else:
pass
except (KeyboardInterrupt, SystemExit):
print '\nkeyboardinterrupt found!'
print '\n...Program Stopped Manually!'
raise
Run Code Online (Sandbox Code Playgroud)
同样,我不确定问题是什么,但我的终端甚至从未打印过我的异常中的两个打印警报.有人能帮我解决这个问题吗?
wbe*_*rry 12
将break语句替换为raise语句,如下所示:
while True:
try:
if subprocess_cnt <= max_subprocess:
try:
notifier.process_events()
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
notifier.stop()
print 'KeyboardInterrupt caught'
raise # the exception is re-raised to be caught by the outer try block
else:
pass
except (KeyboardInterrupt, SystemExit):
print '\nkeyboardinterrupt caught (again)'
print '\n...Program Stopped Manually!'
raise
Run Code Online (Sandbox Code Playgroud)
except块中的两个打印语句应该以'(再次)'出现第二个来执行.
| 归档时间: |
|
| 查看次数: |
30107 次 |
| 最近记录: |