3

I have the below for loop:

for batch in loader:
    # do something with batch
    ...

My loop sometimes fails, while extracting the batch from the loader. What I want to do is something similar to snippet below, but I would like to be able to continue the loop on the next value, rather than skip the rest of the values.

error_idxs = [] 

try:
    for i,batch in enumerate(loader):
        # do something with batch
        ...
except:
    error_idxs.append(i)

The problem with the method above is that it exits out of the loop as soon as an exception happens rather than continuing with the next batch.

Is there a way to continue looping at the next batch?

4
  • just put the try/except inside the loop instead of outside the loop Commented Jan 7, 2021 at 11:05
  • If the loader fails how could you ask it again ? And so when stopping ? Commented Jan 7, 2021 at 11:07
  • When the loader fail, you can ask it again after that error ? quite strange Commented Jan 7, 2021 at 11:07
  • I am trying to capture the error in the loop part of the code. So my error occurs while extracting the batch. Commented Jan 7, 2021 at 11:10

2 Answers 2

5
error_idxs = []
i = 0
while True:
    try:
        batch = next(loader)
        # do something
    except StopIteration:
        break
    except Exception:
        error_idxs.append(i)
    finally:
        i += 1

Edit: Corrected StopIterationError to StopIteration and removed continue

Sign up to request clarification or add additional context in comments.

5 Comments

I think that might just do it. May I ask what the StopIterationError is for?
Is it to exit if the iterator runs out of values? @sunnytown
Yes, next will raise a StopIteration exception when the iterator is exhausted.
StopIteration is raised when the iterable (so the loader) runs out of new values. A for loop automatically catches the StopIteration behind the scenes and uses it to know when it should stop. The while loop doesn't, since it runs forever, therefore we gotta catch it ourselves.
Btw. if you know the exact exception that is raised when your loader has a connection problem, then I recommend you write that error in instead of Exception. If you don't do that, it will catch all possible exceptions, which is something you want to avoid.
0

You may use while loop instead.

Here it will be extracted inside loop so exception can be caught inside loop and handled and continued with rest!

error_idxs = [] 

i = -1
while i < len(loader) -1:
    try:
        i = i + 1
        batch = loader[i]
        do something witth batch
        ...
    except:
        error_idxs.append(i)

2 Comments

Why calculate the length of loader every iteration of the loop? It's not changing in your example.
@Fauxcuss thats not exactly the point of focus here, and yea caching it in a variable would definitely be a lot better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.