在 try except 中收到错误后 while 循环退出

问题描述 投票:0回答:1

我想执行Python代码,但在发生超时错误时,我希望代码在60秒延迟后自动重试并继续。但是,如果发生另一个错误(例如权限问题),代码应退出并显示相应的错误消息。我下面的当前代码无法正确退出并陷入无限循环。我该如何修改代码来解决这个问题?请指教

 pause=60
 while True:
   try:
     df=report.next_records()   ##call the function here
   except:
     sleep(pause)
     pause+=60
   else:
     break  



  
python while-loop except
1个回答
0
投票

捕获特定异常

pause=60
while True:
    try:
        df=report.next_records()   ##call the function here
    except TimeoutError: # or whatever error you're catching
        sleep(pause)
        pause+=60
© www.soinside.com 2019 - 2024. All rights reserved.