2012-10-17 103 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