我有一个从 twitter API 请求信息的程序,有时我会收到错误消息:
IOError: [Errno socket error] [Errno 54] Connection reset by peer
我想知道如何让脚本保持运行(循环)。我知道这与以下内容有关:
try:
except IOError:
但我想不通。
请您参考如下方法:
更简单的结构是这样的:
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
完整的构造可能更复杂,包括 try/except/else/finally。
From an example in docs :
>>> def divide(x, y):
... try:
... result = x / y
... except ZeroDivisionError:
... print "division by zero!"
... else:
... print "result is", result
... finally:
... print "executing finally clause"
