-1

I'm trying to process through my try loop again after an error has been found and handled by except (AttributeError). Right now, the script ends after an error is handled rather than starting from the try loop. Any help getting back to the try loop after an error is encountered would be greatly appreciated.

try:
    #code
except (AttributeError) as error:
    print (error)
pHorseSpec
  • 1,246
  • 5
  • 19
  • 48

3 Answers3

1

You could create a while loop that encompasses everything:

while True:
    try:
        #code
        break
    except (AttributeError) as error:
        print (error)

I added a break at the end of the try statement because I am under the assumption that once all the try code successfully runs you would like that section to complete.

FTA
  • 335
  • 1
  • 7
1
while True:
    try:
        #code
    except AttributeError as error:
        print(error)
    else:
        break

Control goes to the else block only if there is no exception in the try block. Check this: https://docs.python.org/2/tutorial/errors.html#handling-exceptions

rohithvsm
  • 94
  • 5
0

You can do something like this:

retry = False
try:
    #code
except AttributeError as error:
    print error
    retry = True
finally:
    if retry:
        #code    
kakhkAtion
  • 2,264
  • 22
  • 23