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

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

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

android - How to create dynamically Fragment pager adapter -