【发布时间】:2014-08-12 00:13:58
【问题描述】:
我正在尝试使用以下 try-except 块捕获 requests 库引发的一些异常:
try:
get = requests.get((requester.batchesUrl)+str(id)+'/', auth=requester.auth)
except (ConnectionRefusedError, ConnectionError, MaxRetryError) as e:
print("CAUGHT ECONNECTION ERROR")
raise type(e)(str(e) + "Additional Info: Method couldn't connect to website, check that your server is running"
).with_traceback(sys.exc_info()[2])
但是我没有捕获异常并将"Additional Info:" 添加到args,而是得到NameError: global name 'MaxRetryError' is not defined. 现在我知道MaxRetryError 是urllib3.exceptions.MaxRetryError 中的一个异常。我必须导入这些非内置异常才能捕获它们吗?考虑到可能需要注意的可能异常的数量,这对我来说似乎很冗长。
运行: Python 3.3、Windows 7。
【问题讨论】:
标签: python exception-handling python-requests