<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title>Exceptions — Python 3.7.4 documentation</title> <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/language_data.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 3.7.4 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="next" title="Event Loop" href="asyncio-eventloop.html" /> <link rel="prev" title="Queues" href="asyncio-queue.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <link rel="canonical" href="https://docs.python.org/3/library/asyncio-exceptions.html" /> <script type="text/javascript" src="../_static/copybutton.js"></script> <script type="text/javascript" src="../_static/switchers.js"></script> <style> @media only screen { table.full-width-table { width: 100%; } } </style> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="asyncio-eventloop.html" title="Event Loop" accesskey="N">next</a> |</li> <li class="right" > <a href="asyncio-queue.html" title="Queues" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <span class="language_switcher_placeholder">en</span> <span class="version_switcher_placeholder">3.7.4</span> <a href="../index.html">Documentation </a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> »</li> <li class="nav-item nav-item-3"><a href="asyncio.html" accesskey="U"><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code> — Asynchronous I/O</a> »</li> <li class="right"> <div class="inline-search" style="display: none" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('.inline-search').show(0);</script> | </li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="exceptions"> <span id="asyncio-exceptions"></span><h1>Exceptions<a class="headerlink" href="#exceptions" title="Permalink to this headline">¶</a></h1> <dl class="exception"> <dt id="asyncio.TimeoutError"> <em class="property">exception </em><code class="descclassname">asyncio.</code><code class="descname">TimeoutError</code><a class="headerlink" href="#asyncio.TimeoutError" title="Permalink to this definition">¶</a></dt> <dd><p>The operation has exceeded the given deadline.</p> <div class="admonition important"> <p class="admonition-title">Important</p> <p>This exception is different from the builtin <a class="reference internal" href="exceptions.html#TimeoutError" title="TimeoutError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TimeoutError</span></code></a> exception.</p> </div> </dd></dl> <dl class="exception"> <dt id="asyncio.CancelledError"> <em class="property">exception </em><code class="descclassname">asyncio.</code><code class="descname">CancelledError</code><a class="headerlink" href="#asyncio.CancelledError" title="Permalink to this definition">¶</a></dt> <dd><p>The operation has been cancelled.</p> <p>This exception can be caught to perform custom operations when asyncio Tasks are cancelled. In almost all situations the exception must be re-raised.</p> <div class="admonition important"> <p class="admonition-title">Important</p> <p>This exception is a subclass of <a class="reference internal" href="exceptions.html#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a>, so it can be accidentally suppressed by an overly broad <code class="docutils literal notranslate"><span class="pre">try..except</span></code> block:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span> <span class="k">await</span> <span class="n">operation</span> <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span> <span class="c1"># The cancellation is broken because the *except* block</span> <span class="c1"># suppresses the CancelledError exception.</span> <span class="n">log</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="s1">'an error has occurred'</span><span class="p">)</span> </pre></div> </div> <p>Instead, the following pattern should be used:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span> <span class="k">await</span> <span class="n">operation</span> <span class="k">except</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">CancelledError</span><span class="p">:</span> <span class="k">raise</span> <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span> <span class="n">log</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="s1">'an error has occurred'</span><span class="p">)</span> </pre></div> </div> </div> </dd></dl> <dl class="exception"> <dt id="asyncio.InvalidStateError"> <em class="property">exception </em><code class="descclassname">asyncio.</code><code class="descname">InvalidStateError</code><a class="headerlink" href="#asyncio.InvalidStateError" title="Permalink to this definition">¶</a></dt> <dd><p>Invalid internal state of <a class="reference internal" href="asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal notranslate"><span class="pre">Task</span></code></a> or <a class="reference internal" href="asyncio-future.html#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a>.</p> <p>Can be raised in situations like setting a result value for a <em>Future</em> object that already has a result value set.</p> </dd></dl> <dl class="exception"> <dt id="asyncio.SendfileNotAvailableError"> <em class="property">exception </em><code class="descclassname">asyncio.</code><code class="descname">SendfileNotAvailableError</code><a class="headerlink" href="#asyncio.SendfileNotAvailableError" title="Permalink to this definition">¶</a></dt> <dd><p>The “sendfile” syscall is not available for the given socket or file type.</p> <p>A subclass of <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>.</p> </dd></dl> <dl class="exception"> <dt id="asyncio.IncompleteReadError"> <em class="property">exception </em><code class="descclassname">asyncio.</code><code class="descname">IncompleteReadError</code><a class="headerlink" href="#asyncio.IncompleteReadError" title="Permalink to this definition">¶</a></dt> <dd><p>The requested read operation did not complete fully.</p> <p>Raised by the <a class="reference internal" href="asyncio-stream.html#asyncio-streams"><span class="std std-ref">asyncio stream APIs</span></a>.</p> <p>This exception is a subclass of <a class="reference internal" href="exceptions.html#EOFError" title="EOFError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EOFError</span></code></a>.</p> <dl class="attribute"> <dt id="asyncio.IncompleteReadError.expected"> <code class="descname">expected</code><a class="headerlink" href="#asyncio.IncompleteReadError.expected" title="Permalink to this definition">¶</a></dt> <dd><p>The total number (<a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>) of expected bytes.</p> </dd></dl> <dl class="attribute"> <dt id="asyncio.IncompleteReadError.partial"> <code class="descname">partial</code><a class="headerlink" href="#asyncio.IncompleteReadError.partial" title="Permalink to this definition">¶</a></dt> <dd><p>A string of <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> read before the end of stream was reached.</p> </dd></dl> </dd></dl> <dl class="exception"> <dt id="asyncio.LimitOverrunError"> <em class="property">exception </em><code class="descclassname">asyncio.</code><code class="descname">LimitOverrunError</code><a class="headerlink" href="#asyncio.LimitOverrunError" title="Permalink to this definition">¶</a></dt> <dd><p>Reached the buffer size limit while looking for a separator.</p> <p>Raised by the <a class="reference internal" href="asyncio-stream.html#asyncio-streams"><span class="std std-ref">asyncio stream APIs</span></a>.</p> <dl class="attribute"> <dt id="asyncio.LimitOverrunError.consumed"> <code class="descname">consumed</code><a class="headerlink" href="#asyncio.LimitOverrunError.consumed" title="Permalink to this definition">¶</a></dt> <dd><p>The total number of to be consumed bytes.</p> </dd></dl> </dd></dl> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="asyncio-queue.html" title="previous chapter">Queues</a></p> <h4>Next topic</h4> <p class="topless"><a href="asyncio-eventloop.html" title="next chapter">Event Loop</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li> <a href="https://github.com/python/cpython/blob/3.7/Doc/library/asyncio-exceptions.rst" rel="nofollow">Show Source </a> </li> </ul> </div> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="asyncio-eventloop.html" title="Event Loop" >next</a> |</li> <li class="right" > <a href="asyncio-queue.html" title="Queues" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <span class="language_switcher_placeholder">en</span> <span class="version_switcher_placeholder">3.7.4</span> <a href="../index.html">Documentation </a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> »</li> <li class="nav-item nav-item-3"><a href="asyncio.html" ><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code> — Asynchronous I/O</a> »</li> <li class="right"> <div class="inline-search" style="display: none" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('.inline-search').show(0);</script> | </li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 2001-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="https://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 13, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1. </div> </body> </html>