0

I am trying to have try and except block in my code to catch an error, then put it to sleep for 5 seconds and then I want to continue where it left off. Following is my code and currently as soon as it catches exception, it does not continue and stops after exception.

from botocore.exceptions import ClientError

tries = 0

try:
    for pag_num, page in enumerate(one_submitted_jobs):
        if 'NextToken' in page:
            print("Token:",pag_num)
        else:
            print("No Token in page:", pag_num)

except ClientError as exception_obj:
    if exception_obj.response['Error']['Code'] == 'ThrottlingException':
        print("Throttling Exception Occured.")
        print("Retrying.....")
        print("Attempt No.: " + str(tries))
        time.sleep(5)
        tries +=1
    else:
        raise

How can I make it to continue after exception? Any help would be great.

Note - I am trying to catch AWS's ThrottlingException error in my code.

Following code is for demonstration to @Selcuk to show what I have currently from his answer. Following will be deleted as soon as we agree if I am doing it correct or not.

tries = 1
pag_num = 0

# Only needed if one_submitted_jobs is not an iterator:
one_submitted_jobs = iter(one_submitted_jobs)

while True:
    try:
        page = next(one_submitted_jobs)
        # do things
        if 'NextToken' in page:
            print("Token: ", pag_num)
        else:
            print("No Token in page:", pag_num)
        pag_num += 1
    
    except StopIteration:
        break
    
    except ClientError as exception_obj:
        # Sleep if we are being throttled
        if exception_obj.response['Error']['Code'] == 'ThrottlingException':
            print("Throttling Exception Occured.")
            print("Retrying.....")
            print("Attempt No.: " + str(tries))
            time.sleep(3)
            tries +=1 
user9431057
  • 1,203
  • 1
  • 14
  • 28
  • Continue doing _what_? Executing the `for` loop? In that case you should have the `try` inside `for`, not outside. – Selcuk Mar 24 '21 at 04:48
  • @Selcuk yeah execute for loop. I tried inside for loop like mentioned here [SO link](https://stackoverflow.com/a/18994375/6626093) , in that case, it doesn't catch the execption – user9431057 Mar 24 '21 at 04:55
  • That doesn't make any sense. In that case you should be getting the exception in the `for` line. What is `one_submitted_jobs`? Is it a generator? – Selcuk Mar 24 '21 at 04:57
  • You should use any iteration to work it cyclic but when you call the raise exception, it forces you to stop the loop. – mhhabib Mar 24 '21 at 04:57
  • @Selcuk yes, `one_submitted_jobs` is a generator and I am getting an error in `for` loop. Not sure how to continue – user9431057 Mar 24 '21 at 04:59
  • @toRex I understand your point, but as per my comment above, if I do that cyclic, it doesn't catch the exception. – user9431057 Mar 24 '21 at 05:07
  • @user9431057 give a try by skipping raise and alternately use any print function then follow this [iteration](https://docs.python.org/3/tutorial/errors.html#handling-exceptions) – mhhabib Mar 24 '21 at 05:10
  • @Selcuk and #toRex I tried `while True:/break` and it also stops after exception happens :( But for sure without break it continues for infinite loop.. – user9431057 Mar 24 '21 at 05:24

1 Answers1

0

You are not able to keep running because the exception occurs in your for line. This is a bit tricky because in this case the for statement has no way of knowing if there are more items to process or not.

A workaround could be to use a while loop instead:

pag_num = 0
# Only needed if one_submitted_jobs is not an iterator:
one_submitted_jobs = iter(one_submitted_jobs)   
while True:
    try:
        page = next(one_submitted_jobs)
        # do things
        pag_num += 1
    except StopIteration:
        break
    except ClientError as exception_obj:
        # Sleep if we are being throttled
Selcuk
  • 57,004
  • 12
  • 102
  • 110
  • Appreciate your time, however, when I do this, it still stops after it finds first Client Error exception. I am clueless now :( – user9431057 Mar 24 '21 at 05:29
  • Stops and does what? Does it break out of the `while` loop? – Selcuk Mar 24 '21 at 05:57
  • I think with your method, we are sooo close. Only thing is as soon as `except ClientError as exception_obj` happens, we need to still continue until we have gone through all objects in generator/iterator. As of now, after the Client Exception, while loop break and does nothing just stops the execution. – user9431057 Mar 24 '21 at 06:16
  • There is nothing that could break the loop in the `except ClientError...` block. Are you sure you are executing the `ThrottlingException` clause and not the `else` part? – Selcuk Mar 24 '21 at 06:27
  • I am sure I did what you asked me to do. I posted the code I have for your reference. Please have a look when you can and give me a comment. – user9431057 Mar 24 '21 at 06:37