简体   繁体   English

Python 捕获异常并继续

[英]Python caught exception and continue

I've a little script in python that work in loop.我在 python 中有一个循环工作的小脚本。 Every 30 seconds it get an url with requests to check if content of the page is changed.每 30 秒它会收到一个 url requests检查页面内容是否更改。

But sometime I get script error (about 1 time a day):但有时我会收到脚本错误(每天大约 1 次):

ConnectionError: HTTPSConnectionPool(host='www.example.com', port=443): Max retries exceeded with url: /test/ (Caused by NewConnectionError(<urllib3.connection.VerifiedHTTPSConnection object at 0x.......>: [Errno -3] Temporary failure in name resolution))

What it the best way to intercept exception and, if occurs, wait another 30 seconds and continue the script instead stop it?拦截异常的最佳方法是什么,如果发生,再等待 30 秒并继续脚本而不是停止它? The exception to caught is ConnectionError or NewConnectionError ?捕获的异常是ConnectionErrorNewConnectionError

Put a try/except around your code, like this:在您的代码周围放置一个 try/except ,如下所示:

try:

# your code here

except ConnectionError:
    pass
except NewConnectionError:
    pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM