如何在使用ruby u时获得异常后在python中重试循环
begin
a.each do |s|
end
rescue
sleep 2
retry
end
我将使用睡眠时间,并在获得excpetion后使用重试循环,现在我想在python中执行此操作。
我知道用于异常处理的代码
try:
for i in w:
#Loop code
exception e:
如果有异常如何重试循环?#如果有异常如何重试循环?
发布于 2015-04-07 12:18:07
您可以创建一个函数并调用它:
def my_func():
try:
for i in w:
#Loop code
except:
# here take care of exception, try to fix it, when it occurs
my_funct()
如果您不想重新开始:
try:
for i in w:
#Loop code
except:
# here take care of exception, try to fix it, when it occurs
发布于 2017-03-13 08:01:43
看起来你要找的是一个递归函数。像这样,
# retries = 3
def do(x):
try:
y = something(x)
except Exception as err:
# while retries > 0:
# retries -= 1
do(x)
else:
print(y)
注意,这将创建一个无穷无尽的重试循环,除非您以某种方式设置了一个像我注释掉的那样的限制器。
https://stackoverflow.com/questions/29483594
复制相似问题