2

在您使用的ruby中获得异常后如何在python中重试循环

begin
    a.each do |s|
    end
rescue
      sleep 2
        retry 
end

我将使用睡眠时间并在获得异常后使用重试循环现在我想在 python 中执行此操作

我知道异常处理的代码

try:
   for i in w:
   #Loop code
exception e:

如果有任何异常,如何重试循环?#如果发生异常,如何重试循环

4

2 回答 2

-1

您可以创建一个函数并调用它:

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
于 2015-04-07T04:18:07.240 回答
-1

您正在寻找的是一个递归函数。就像是,

# retries = 3
def do(x):
    try:
        y = something(x)
    except Exception as err:
        # while retries > 0:
        #    retries -= 1
            do(x)    
    else:
        print(y)

请注意,这将创建一个无限循环的重试,除非您以某种方式设置了一个限制器,就像我注释掉的那个一样。

于 2017-03-13T00:01:42.793 回答