有没有办法降低异常,以便在异常发生和程序退出之前执行 try 语句的其余部分?
while(True):
try:
do something 1
error occurs here
do something 2
except error as e:
if(iteration == 2):
iteration += 1
# here I want to go back to do something 2 instead of exit()
exit()
最佳答案
If finally is present, it specifies a ‘cleanup’ handler. The try clause is executed, including any except and else clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The finally clause is executed. If there is a saved exception, it is re-raised at the end of the finally clause. If the finally clause raises another exception or executes a return or break statement, the saved exception is discarded:
def f():
try:
1/0
finally:
return 42
>>> f()
42
关于python - 手动降低Python中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31344816/