解决龙卷风异常

约翰·B

嗨,我有以下代码:

    httpClient=AsyncHTTPClient()
    try:
       response=yield httpClient.fetch("http://ww.bbc.co.uk/news/uk-000")

    except Exception as e:
       print(e)

我有意获取一个无效的URL来确定如何处理错误。有了以上我得到:

[Errno 8] nodename nor servname provided, or not known

但是我试图找出这是什么异常类,所以我做了以下工作:

    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

首先,我不明白为什么这段代码中的type(e)不能告诉我这是什么类型的Exception类?

其次,龙卷风文档指出抛出了HTTPError,但是它看起来并不涵盖所有可以抛出的Exception。

谢谢

本·达内尔

您是否还有另一个名为的变量type正在遮盖内置函数type

HTTPError当服务器返回非200响应代码时,文档状态会引发,但是如果我们没有做到那么远,则可能会引发其他异常。这是一个socket.gaierror,但是对筹集的金额没有限制,因此您必须使用它except Exception来捕获所有内容。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章