如何以 Pythonic 方式停止从 try/except 执行 for 循环的代码?

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

我有一些 Python 代码如下:

for emailCredentials in emailCredentialsList:
   try:
       if not emailCredentials.valid:
           emailCredentials.refresh()
   except EmailCredentialRefreshError as e:
       emailCredentials.active = False
       emailCredentials.save()
       # HERE I WANT TO STOP THIS ITERATION OF THE FOR LOOP 
       # SO THAT THE CODE BELOW THIS DOESN'T RUN ANYMORE. BUT HOW?

   # a lot more code here that scrapes the email box for interesting information

正如我已经在代码中评论的那样,如果抛出 EmailCredentialRefreshError,我希望 for 循环的这次迭代停止并移至 emailCredentialsList 中的下一项。我不能使用 break 因为那会停止整个循环并且不会覆盖循环中的其他项目。我当然可以将所有代码包装在 try/except 中,但我希望将它们放在一起以便代码保持可读性。

最Python解决这个问题的方法是什么?

尝试使用 continue 语句。这将继续到循环的下一次迭代。

for emailCredentials in emailCredentialsList:
   try:
       if not emailCredentials.valid:
           emailCredentials.refresh()
   except EmailCredentialRefreshError as e:
       emailCredentials.active = False
       emailCredentials.save()
       continue
   <more code>