<p>从包含传递给该异常的参数的基异常类<a href="https://docs.python.org/3/library/exceptions.html#BaseException.args" rel="nofollow noreferrer">has</a>和<code>args</code>属性(类型为<code>tuple</code>)派生的每个行为良好的异常。大多数情况下,只有一个参数传递给异常,可以使用<code>args[0]</code>访问。</p>
<p>Tweepy传递给其异常的参数具有类型为<code>List[dict]</code>的结构。可以使用以下代码从参数获取错误代码(类型<code>int</code>)和错误消息(类型<code>str</code>):</p>
<pre><code>e.args[0][0]['code']
e.args[0][0]['message']
</code></pre>
<p><a href="https://github.com/tweepy/tweepy/blob/master/tweepy/error.py" rel="nofollow noreferrer">TweepError exception class</a>还提供了几个附加的有用属性<code>api_code</code>、<code>reason</code>和<code>response</code>。它们之所以是<a href="https://tweepy.readthedocs.io/en/latest/api.html" rel="nofollow noreferrer">not documented</a>是因为某些原因,即使它们是公共API的一部分。</p>
<p>因此,还可以使用以下代码获取错误代码(类型<code>int</code>):</p>
<pre><code>e.api_code
</code></pre>
<p><br/></p>
<h2>历史记录:</h2>
<p>以前使用<code>e.message[0]['code']</code>访问的错误代码不再工作。在Python 3.0中,<code>message</code>属性已被<a href="https://www.python.org/dev/peps/pep-0352/" rel="nofollow noreferrer">deprecated in Python 2.6</a>删除。当前出现错误<code>'TweepError' object has no attribute 'message'</code>。</p>