即使IOError,如何继续python脚本循环?

Jim*_*asa 1 python twitter exception-handling exception

我有一个程序,它从twitter API请求信息,并且我不时收到错误:

IOError: [Errno socket error] [Errno 54] Connection reset by peer
Run Code Online (Sandbox Code Playgroud)

我想知道如何让脚本保持运行(循环).我知道这与此有关:

try:

except IOError:
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚.

joa*_*uin 6

更简单的结构是这样的:

my_while_or_for_loop:
    some_code_here_maybe
    try:
        my_code_or_function_that_sometimes_fails()
    except IOError:
        pass   # or some code to clean things that went wrong or to log the failure
    some_more_code_here_maybe
Run Code Online (Sandbox Code Playgroud)

你想阅读文档

完整的结构可能更复杂,包括try/except/else/finally.
从文档中的示例:

>>> def divide(x, y):
...     try:
...         result = x / y
...     except ZeroDivisionError:
...         print "division by zero!"
...     else:
...         print "result is", result
...     finally:
...         print "executing finally clause"
Run Code Online (Sandbox Code Playgroud)