python之在 while 循环中处理异常

over140 阅读:101 2024-05-10 16:29:43 评论:0

关闭。这个问题需要debugging details .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

5年前关闭。




Improve this question




如果网页尚未加载,我正在调用一个将引发异常的函数。我想等待 2 秒钟,然后再试一次,直到页面加载完毕。

我试试这个:

while(True): 
    try: 
        some_funciont() 
        break 
    except: 
        time.sleep(2) 

但它在第一次迭代后逃脱。

如果未引发异常,我该如何转义?

请您参考如下方法:

尝试这样的事情:

def some_function(){ 
    try: 
        #logic to load the page. If it is successful, it will not go to except. 
        return True 
    except: 
        #will come to this clause when page will throw error. 
        return False 
    } 
     
while(True): 
    if some_function(): 
        break 
    else: 
        time.sleep(2) 
        continue 


标签:Python
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号