python - How to stop code execution of for loop from try/except in a Pythonic way? -


i've got python code follows:

for emailcredentials in emailcredentialslist:    try:        if not emailcredentials.valid:            emailcredentials.refresh()    except emailcredentialrefresherror e:        emailcredentials.active = false        emailcredentials.save()        # here want stop iteration of loop         # code below doesn't run anymore. how?     # lot more code here scrapes email box interesting information 

and commented in code, if emailcredentialrefresherror thrown want iteration of loop stop , move next item in emailcredentialslist. can't use break because stop whole loop , wouldn't cover other items in loop. can of course wrap code in try/except, keep them close code remains readable.

what pythonic way of solving this?

try using continue statement. continues next iteration of loop.

for emailcredentials in emailcredentialslist:    try:        if not emailcredentials.valid:            emailcredentials.refresh()    except emailcredentialrefresherror e:        emailcredentials.active = false        emailcredentials.save()        continue    <more code> 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -