【问题标题】:Re-running a python code with a while loop使用 while 循环重新运行 python 代码
【发布时间】:2021-06-23 22:47:02
【问题描述】:

我如何编写一段代码来识别是否发生了任何类型的错误,如果发生了,代码只是在循环中运行。我会在<some error occurs> 中放置什么函数,以便识别任何错误 0 输出并在 10 秒内重新运行代码。

while True:
    <all your code>

    if <some error occurs>:
        time.sleep(10)
        continue

【问题讨论】:

    标签: python error-handling while-loop compiler-errors output


    【解决方案1】:

    只需使用try...except

    while True:
        try:
            <all_your_code>
        except:
            time.sleep(10)
            continue
    

    阅读本文以更好地理解:https://www.programiz.com/python-programming/exception-handling

    【讨论】:

      猜你喜欢
      • 2019-10-16
      • 1970-01-01
      • 2014-01-30
      • 1970-01-01
      • 2020-02-22
      • 2012-02-26
      • 2021-10-01
      • 2020-04-29
      • 2018-02-01
      相关资源
      最近更新 更多