在while循环中处理异常

Lui*_*uez 3 python exception while-loop

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

我尝试这样:

while(True):
    try:
        some_funciont()
        break
    except:
        time.sleep(2)
Run Code Online (Sandbox Code Playgroud)

但它在第一次迭代后会逸出。

如果未引发异常,该如何逃生?

Dec*_*eca 5

尝试这样的事情:

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
Run Code Online (Sandbox Code Playgroud)