【问题标题】:Handling exceptions in while loop - Python在while循环中处理异常 - Python
【发布时间】:2012-10-17 18:17:03
【问题描述】:

这是我的代码(@cdhowie 的几乎完整版 :)):

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

【问题讨论】:

  • 您的堆栈跟踪与示例代码中给出的调用不匹配。请发布您实际使用的代码,而不是经过消毒的伪代码。 (仅在必要时进行消毒。)
  • 如果将 except Execption as e 更改为 except BaseException as e 会发生什么? (仅用于调试目的
  • @mgilson:我看了一眼pygithub source。该自定义异常确实是 Exception 的子类
  • @jdi -- 唷。如果他们改为子类BaseException,我将不得不非常沮丧:)
  • github.GithubException 派生自 Exception 所以这不可能是您的真实代码,或者它正在其他地方发生。 getUsernames 是从哪里调用的?

标签: python exception exception-handling try-catch


【解决方案1】:

仔细查看异常中的堆栈跟踪:

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 请求不会完全按照您的预期发生。

为了捕获和处理此异常,您需要使用 try/except 处理程序将代码封装在 getUsernames 函数中。

【讨论】:

  • 我可以在这个循环中以某种方式做到这一点吗?例如,通过在匿名函数中确定范围?
猜你喜欢
  • 1970-01-01
  • 2018-11-13
  • 2014-11-11
  • 1970-01-01
  • 2013-09-21
  • 2017-07-06
  • 2016-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多