繁体   English   中英

异常处理-Python中的ruby重试循环

[英]exception handling - ruby retry loop in python

如何在Ruby中获取异常后重试Python中的循环

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

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

我知道用于异常处理的代码

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

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

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

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

您似乎在寻找的是递归函数。 就像是,

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

请注意,这将导致无休止的重试循环,除非您以某种方式设置一个限制器(如我注释掉的限制器)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM