2015-11-14 38 views
0

您好我有以下代码:工作了龙卷风例外

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 

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

其次,龙卷风文档声明HTTPError被抛出,但它看起来并不像所有可以抛出的Exception。

感谢

回答

2

你有一个名为type被遮蔽的内置功能type另一个变量?

该文档声明HTTPError是在服务器返回非200响应代码时引发的,但如果我们没有达到那么远,则可能会引发其他异常。这是一个socket.gaierror,但是对于可能会产生的内容没有限制,所以您必须使用except Exception来捕捉所有内容。

+0

本感谢。我现在感觉像是一个白痴......我正在使用一个叫做type:S的变量,并且为什么这样做不起作用而变得疯狂...... –