擅长:python、mysql、java
<p>如您所见,如果文档不存在,则在调用<code>fetch()</code>时会引发异常。但它将在除块中处理。如果错误代码不是404,则将重新引发异常。所以对于404以外的所有错误代码,您将得到一个异常。你知道吗</p>
<pre><code>def __enter__(self):
"""
Supports context like editing of document fields. Handles context
entry logic. Executes a Document.fetch() upon entry.
"""
# We don't want to raise an exception if the document is not found
# because upon __exit__ the save() call will create the document
# if necessary.
try:
self.fetch()
except HTTPError as error:
if error.response.status_code != 404:
raise
return self
</code></pre>