嗨,我有以下代码:
httpClient=AsyncHTTPClient()
try:
response=yield httpClient.fetch("http://ww.bbc.co.uk/news/uk-000")
except Exception as e:
print(e)
我故意获取一个无效的URL来解决如何处理错误。有了以上我得到:
^{pr2}$但是,我试图找出这是什么异常类,所以我做了以下操作:
httpClient=AsyncHTTPClient()
try:
response=yield httpClient.fetch("http://ww.bbc.co.uk/news/uk-000")
except Exception as e:
print(type(e))
我得到:
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tornado-4.1-py3.4-macosx-10.6-intel.egg/tornado/gen.py", line 810, in run
yielded = self.gen.throw(*sys.exc_info())
File "messages.py", line 230, in on_message
print(type(e))
TypeError: 'str' object is not callable
为什么这个类型的代码我不明白?在
第二,tornado文档声明抛出了HTTPError,但是它看起来并没有涵盖所有可以抛出的异常。在
谢谢
您是否有另一个名为
type
的变量来隐藏内置函数type
?在文档声明,当服务器返回非200响应代码时,
HTTPError
将被引发,但是如果我们没有做到这一点,那么可能会引发其他异常。这是一个socket.gaierror
,但是对可能产生的内容没有限制,所以必须使用except Exception
来捕获所有内容。在相关问题 更多 >
编程相关推荐