2012-11-07 38 views
4

常規例外捕手記錄以下異常:如何知道在python中捕獲哪種異常類型?

> Traceback (most recent call last): File "4sq.py", line 37, in 
> <module> 
>  checkin = client.checkins() File "/usr/local/lib/python2.7/dist-packages/foursquare-20120716-py2.7.egg/foursquare/__init__.py", 
> line 416, in __call__ 
>  return self.GET('{CHECKIN_ID}'.format(CHECKIN_ID=CHECKIN_ID), params, multi=multi) File 
> "/usr/local/lib/python2.7/dist-packages/foursquare-20120716-py2.7.egg/foursquare/__init__.py", 
> line 217, in GET 
>  return self.requester.GET(self._expanded_path(path), *args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/foursquare-20120716-py2.7.egg/foursquare/__init__.py", 
> line 163, in GET 
>  return self._request(url) File "/usr/local/lib/python2.7/dist-packages/foursquare-20120716-py2.7.egg/foursquare/__init__.py", 
> line 200, in _request 
>  return _request_with_retry(url, data)['response'] File "/usr/local/lib/python2.7/dist-packages/foursquare-20120716-py2.7.egg/foursquare/__init__.py", 
> line 696, in _request_with_retry 
>  return _process_request_with_httplib2(url, data) File "/usr/local/lib/python2.7/dist-packages/foursquare-20120716-py2.7.egg/foursquare/__init__.py", 
> line 719, in _process_request_with_httplib2 
>  return _check_response(data) File "/usr/local/lib/python2.7/dist-packages/foursquare-20120716-py2.7.egg/foursquare/__init__.py", 
> line 742, in _check_response 
>  raise exc(meta.get('errorDetail')) RateLimitExceeded: Quota exceeded 

我想知道具體的異常名,所以我可以專門捕捉到它。 如何找到?

是否有對陷入異常的「類型」的功能,還是應該在投擲LIB的源中找到 - 可用here

+6

看起來就像是一個'RateLimitExceeded'例外。雖然,可以肯定的是,我會將它封裝在'try' /'中,除了Exception as e'並打印'e .__ class__'。那會給你一個明確的答案 – inspectorG4dget

+0

謝謝你正在尋找類似e .__ class__的東西! – user971956

回答

2

例外在貼被提出是foursquare.RateLimitExceeded(因爲它在最後一行說) 。您應該能夠像平常一樣捕獲它,或者如果要處理來自模塊的所有錯誤,請捕獲它的基類foursquare.FoursquareException

引發異常的代碼只是查找從字典中引發哪個異常類。這應該對你如何捕捉這些錯誤沒有任何影響。

2

這本來是一個評論,但因爲它得到了很多upvotes和OP聲稱它是什麼,他們正在尋找,我重新張貼它作爲一個答案:

它看起來就像是一個RateLimitExceeded例外。不過,如果你真的要肯定的是,你可以這樣做:

try: 
    # code 
except Exception as e: 
    print e.__class__ 

這將打印出時引發的異常類,它會給你一個明確的答案