当前位置: 首页 > 面试题库 >

您可以“重新启动” Python循环的当前迭代吗?

东门修文
2023-03-14
问题内容

有没有办法实现这样的事情:

for row in rows:
    try:
        something
    except:
        restart iteration

问题答案:

您可以将try/except块放入另一个循环,然后在成功时中断:

for row in rows:
    while True:
        try:
            something
            break
        except Exception: # Try to catch something more specific
            pass


 类似资料: