异常后如何重试? [英] How to retry after exception?
本文介绍了异常后如何重试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个以for i in range(0, 100)
开头的循环.正常情况下,它可以正常运行,但有时由于网络条件而失败.目前,我已对其进行了设置,以便在失败时将在except子句中显示为continue
(继续使用i
的下一个数字).
I have a loop starting with for i in range(0, 100)
. Normally it runs correctly, but sometimes it fails due to network conditions. Currently I have it set so that on failure, it will continue
in the except clause (continue on to the next number for i
).
是否可以将相同的数字重新分配给i
并再次执行失败的循环迭代?
Is it possible for me to reassign the same number to i
and run through the failed iteration of the loop again?
推荐答案
在for循环内执行while True
,将try
代码放入内部,仅在代码成功后才从while
循环中断. /p>
Do a while True
inside your for loop, put your try
code inside, and break from that while
loop only when your code succeeds.
for i in range(0,100):
while True:
try:
# do stuff
except SomeSpecificException:
continue
break
这篇关于异常后如何重试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文