我知道 try/except 可以处理我程序中的错误。 但是,有没有办法让程序执行时显示错误,忽略并继续执行呢?
最佳答案
在 VBScript 和其他 VB 派生语言中,您可以使用“ON ERROR GOTO NEXT”获得这种行为。
Python 中不存在这样的行为。即使你像这样包装每个顶级语句:
try:
do_something()
except Exception as e:
print e
try:
do_something_else()
except Exception as e:
print e
在抛出异常时,您仍然会得到 do_something 中的语句被跳过的结果。
不过,如果您有特定的用例,可能还有其他可以接受的答案。例如,在顶层循环中:
while True:
cmd = get_command()
if cmd == 'quit': break
try:
run_command(cmd)
except Exception as e:
print "Error running " + cmd + ":"
print e
关于python - 如何看到错误并仍然在 Python shell 中保持程序运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3950505/