Skipping Iterations in Python


Question

I have a loop going, but there is the possibility for exceptions to be raised inside the loop. This of course would stop my program all together. To prevent that I catch the exceptions and handle them. But then the rest of the iteration runs even though an exception occurred. Is there a keyword to use in my except: clause to just skip the rest of the current iteration?

1
127
7/22/2019 7:46:10 PM

Accepted Answer

You are looking for continue.

324
8/3/2018 9:59:02 AM

for i in iterator:
    try:
        # Do something.
        pass
    except:
        # Continue to next iteration.
        continue

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon