A Merii
A Merii

Reputation: 594

Catch Exception in for Loop Python

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?

Upvotes: 3

Views: 1244

Answers (2)

sunnytown
sunnytown

Reputation: 1996

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

Upvotes: 5

Jaysmito Mukherjee
Jaysmito Mukherjee

Reputation: 1526

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)

Upvotes: 0

Related Questions