Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在while-loop-Python中处理异常_Python_Exception_Exception Handling_Try Catch - Fatal编程技术网

在while-loop-Python中处理异常

在while-loop-Python中处理异常,python,exception,exception-handling,try-catch,Python,Exception,Exception Handling,Try Catch,这是我的代码(几乎是@cdhowie:)的完整版本): 问题是,出现了异常,但我无法处理它。以下是一个输出: ### 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/fun

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

问题是,出现了异常,但我无法处理它。以下是一个输出:

### 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'}
###循环
###努力。。。
回溯(最近一次呼叫最后一次):
文件“main.py”,第12行,在
usernames=getUsernames(位置,gh)
getUsernames中的文件“/home/ciembor/projekty/github rank/functions.py”,第39行
对于结果中的用户:
文件“/usr/lib/python2.7/site packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py”,第33行,在__
newElements=self.\uu grow()
文件“/usr/lib/python2.7/site packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py”,第45行,在
newElements=self.\u fetchNextPage()
文件“/usr/lib/python2.7/site packages/PyGithub-1.8.0-py2.7.egg/github/Legacy.py”,第37行,在_fetchNextPage
返回自我获取页面(第页)
文件“/usr/lib/python2.7/site packages/PyGithub-1.8.0-py2.7.egg/github/Legacy.py”,第48行,在get_页
没有一个
requestAndCheck中的文件“/usr/lib/python2.7/site packages/PyGithub-1.8.0-py2.7.egg/github/Requester.py”,第69行
引发GithubException.GithubException(状态,输出)
github.GithubException.GithubException:403{u'message':超过了11.11.11.11'}的u'API速率限制

为什么不打印
###处理异常

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

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()
  ...
回溯(最近一次呼叫最后一次):
文件“main.py”,第12行,在
usernames=getUsernames(位置,gh)
getUsernames中的文件“/home/ciembor/projekty/github rank/functions.py”,第39行
对于结果中的用户:
文件“/usr/lib/python2.7/site packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py”,第33行,在__
newElements=self.\uu grow()
...
getResult
完成执行后,result:中用户的行
调用的代码引发异常。这意味着您正在使用的API使用的是惰性计算,因此实际的API请求不会在您期望的时候发生


为了捕获和处理此异常,您需要在
getUsernames
函数中使用
try
/
异常处理程序以外的处理程序来包装代码。

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

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()
  ...
回溯(最近一次呼叫最后一次):
文件“main.py”,第12行,在
usernames=getUsernames(位置,gh)
getUsernames中的文件“/home/ciembor/projekty/github rank/functions.py”,第39行
对于结果中的用户:
文件“/usr/lib/python2.7/site packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py”,第33行,在__
newElements=self.\uu grow()
...
getResult
完成执行后,result:
中用户的行
调用的代码引发异常。这意味着您正在使用的API使用的是惰性计算,因此实际的API请求不会在您期望的时候发生


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

堆栈跟踪与示例代码中给出的调用不匹配。请发布您实际使用的代码,而不是经过消毒的伪代码。(仅在必要时进行清理。)如果将
除Execption外的选项更改为e
除BaseException外的选项更改为e
,会发生什么情况?(仅用于调试目的)@mgilson:我看了一眼。该自定义异常没有子类
exception
@jdi--phew。如果将它们子类化为
BaseException
,我会非常不安:)
github。GithubException
是从
Exception
派生的,所以这不可能是您的真实代码,或者它发生在其他地方。从何处调用
getUsernames
?堆栈跟踪与示例代码中给出的调用不匹配。请发布您实际使用的代码,而不是经过消毒的伪代码。(仅在必要时进行清理。)如果将
除Execption外的选项更改为e
除BaseException外的选项更改为e
,会发生什么情况?(仅用于调试目的)@mgilson:我看了一眼。该自定义异常没有子类
exception
@jdi--phew。如果将它们子类化为
BaseException
,我会非常不安:)
github。GithubException
是从
Exception
派生的,所以这不可能是您的真实代码,或者它发生在其他地方。
getUsernames
从哪里调用?我可以在这个循环中以某种方式执行吗?例如,通过在匿名函数中确定它的作用域?我能在这个循环中以某种方式做到这一点吗?例如,通过在匿名函数中确定其范围?