2

How to retry a loop in python after getting the exception in ruby u use

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

I will use sleep time and just use retry the loop after getting excpetion now i want to do this in python

I know code for exception handling

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

How to retry the loop if there is any exception? #How to retry the loop if any exception occurs

Mounarajan
  • 1,357
  • 5
  • 22
  • 43

2 Answers2

-1

you can create a function and call it:

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()  

if you don't want to start again:

try:
    for i in w:
        #Loop code
    except:
        # here take care of exception, try to fix it, when it occurs
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • lets say loop is processing at 1400 array if error occurs the function will start from 0th array right...instead i want to retry that particular array of loop(1400) – Mounarajan Apr 07 '15 at 04:35
  • Thats why in comment i said take care of exception and fix it – Hackaholic Apr 07 '15 at 04:38
-1

What it seems your looking for is a recursive function. Something like,

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

Note this will create an endless loop of retries unless you set a limiter somehow like the one I commented out.

Dan Temkin
  • 1,565
  • 1
  • 14
  • 18