【发布时间】:2012-11-08 20:30:32
【问题描述】:
我有一个程序需要在触发某个条件后完全停止处理 - 类似于这个问题:How do I abort the execution of a Python script?
但是,就我而言,我想捕获 sys.exit() 生成的回溯,并且最重要的是停止处理超出该点的任何代码块。有人可以帮忙吗?
try:
a = 'i' / 1
print 'Does this work yet?'
except TypeError:
print "I should stop processing now"
try:
sys.exit()
except:
pass
print 'I did not stop processing '
【问题讨论】:
-
我也尝试过 try-except 代码块、嵌套 try-except 代码块和引发 SystemExit。