2012-10-17 102 views
1

這裏是我的代碼(幾乎完整版@cdhowie :)):處理異常 - Python的

def getResult(method, argument=None): 
    result = None 

    while True: 
    print('### loop') 
    try: 
     print ('### try hard...') 
     if argument: 
     result = method(argument) 
     else: 
     result = method() 
     break 
    except Exception as e: 
     print('### GithubException') 
     if 403 == e.status: 
     print('Warning: ' + str(e.data)) 
     print('I will try again after 10 minutes...') 
     else: 
     raise e 

    return result 

def getUsernames(locations, gh): 
    usernames = set() 

    for location in locations: 
    print location 
    result = getResult(gh.legacy_search_users, location) 
    for user in result: 
     usernames.add(user.login) 
     print user.login, 

    return usernames 

# "main.py" 
gh = Github() 
locations = ['Washington', 'Berlin'] 
# "main.py", line 12 is bellow 
usernames = getUsernames(locations, gh) 

的問題是,引發異常,但我不能處理它。以下是輸出:

### loop 
### try hard... 
Traceback (most recent call last): 
    File "main.py", line 12, in <module> 
    usernames = getUsernames(locations, gh) 
    File "/home/ciembor/projekty/github-rank/functions.py", line 39, in getUsernames 
    for user in result: 
    File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py", line 33, in __iter__ 
    newElements = self.__grow() 
    File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py", line 45, in __grow 
    newElements = self._fetchNextPage() 
    File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/Legacy.py", line 37, in _fetchNextPage 
    return self.get_page(page) 
    File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/Legacy.py", line 48, in get_page 
    None 
    File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/Requester.py", line 69, in requestAndCheck 
    raise GithubException.GithubException(status, output) 
github.GithubException.GithubException: 403 {u'message': u'API Rate Limit Exceeded for 11.11.11.11'} 

爲什麼不打印### handling exception

+6

您的堆棧跟蹤與示例代碼中給出的調用不匹配。請發佈您實際使用的代碼,而不是經過消毒的僞代碼。 (只在需要時才進行清理。) – cdhowie

+0

如果將'Execption除外e'更改爲'除BaseException as e'外,會發生什麼? (*只使用這個用於調試*) – mgilson

+0

@mgilson:我把偷看的[pygithub源(https://github.com/jacquev6/PyGithub/blob/master/github/GithubException.py#L15)。該自定義異常的子類是'Exception' – jdi

回答

6

仔細看在異常堆棧跟蹤:

Traceback (most recent call last): 
    File "main.py", line 12, in <module> 
    usernames = getUsernames(locations, gh) 
    File "/home/ciembor/projekty/github-rank/functions.py", line 39, in getUsernames 
    for user in result: 
    File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py", line 33, in __iter__ 
    newElements = self.__grow() 
    ... 

異常正從代碼拋出由線for user in result:被稱爲後getResult完成執行。這意味着您使用的API使用惰性評估,所以實際的API請求在您期望的時候並不會發生。

爲了捕獲並處理這個異常,您需要使用/except處理程序將getUsernames函數中的代碼打包。

+0

我可以在此循環中以某種方式做到這一點嗎?例如通過在匿名函數中進行範圍映射? – ciembor