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

即使出现IOError,如何继续python脚本循环?,python,exception,exception-handling,twitter,Python,Exception,Exception Handling,Twitter,我有一个从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_functi

我有一个从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:
    <code that can fail>
except IOError:
    pass
你想

完整的构造可能更复杂,包括
try/except/else/finally

:


较简单的结构如下:

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:
    <code that can fail>
except IOError:
    pass
你想

完整的构造可能更复杂,包括
try/except/else/finally

:

试试这个:

try:
    <do something - you code here>
except IOError: pass
试试看:
除了IOError:通过
试试这个:

try:
    <do something - you code here>
except IOError: pass
试试看:
除了IOError:通过

这是关于

简单地说,如果代码块在某些情况下可能导致某些已知错误(如输入输出错误),则可以定义一个
try except
块来处理此类错误。这将使您的脚本保持运行,并允许您根据不同的错误状态执行不同的代码块。。。。比如:

try:
    <do something>
except IOError:
    <an input-output error occured, do this...>
except ValueError:
    <we got something diffrent then we expected, do something diffrent>
except LookupError:
    pass # we do not need to handle this one, so just kkeep going...
except: 
    <some diffrent error occured, do somethnig more diffrent> 

这是关于……的文件

简单地说,如果代码块在某些情况下可能导致某些已知错误(如输入输出错误),则可以定义一个
try except
块来处理此类错误。这将使您的脚本保持运行,并允许您根据不同的错误状态执行不同的代码块。。。。比如:

try:
    <do something>
except IOError:
    <an input-output error occured, do this...>
except ValueError:
    <we got something diffrent then we expected, do something diffrent>
except LookupError:
    pass # we do not need to handle this one, so just kkeep going...
except: 
    <some diffrent error occured, do somethnig more diffrent> 

您缺少的部分是
pass
。这是一个简单的no-op表达式,它的存在是因为Python不能有空块

较长的解释是: 您需要做的是捕获抛出的IOError异常,并使用
pass
忽略它(可能记录它等等)

要做到这一点,您需要在
块中包装可能失败的代码,如下所示:

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:
    <code that can fail>
except IOError:
    pass
试试看:
<可能失败的代码>
除IOError外:
通过
这样做的目的是显式忽略IOErrors,而不是忽略其他IOErrors。如果要忽略所有异常,只需删除
IOError
部分,使行显示
except:


您应该真正阅读Python教程,特别是关于的部分。

您缺少的部分是
pass
。这是一个简单的no-op表达式,它的存在是因为Python不能有空块

较长的解释是: 您需要做的是捕获抛出的IOError异常,并使用
pass
忽略它(可能记录它等等)

要做到这一点,您需要在
块中包装可能失败的代码,如下所示:

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:
    <code that can fail>
except IOError:
    pass
试试看:
<可能失败的代码>
除IOError外:
通过
这样做的目的是显式忽略IOErrors,而不是忽略其他IOErrors。如果要忽略所有异常,只需删除
IOError
部分,使行显示
except:

您应该真正阅读Python教程,特别是关于。

或为什么不:

with ignored(IOError):
    <code that can fail>
已忽略(IOError):
<可能失败的代码>
或者为什么不:

with ignored(IOError):
    <code that can fail>
已忽略(IOError):
<可能失败的代码>