gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1537 lines
134KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="Built-in Exceptions" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/exceptions.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="In Python, all exceptions must be instances of a class that derives from BaseException. In a try statement with an except clause that mentions a particular class, that clause also handles any excep..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="In Python, all exceptions must be instances of a class that derives from BaseException. In a try statement with an except clause that mentions a particular class, that clause also handles any excep..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>Built-in Exceptions &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="Text Processing Services" href="text.html" />
  33. <link rel="prev" title="Built-in Types" href="stdtypes.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/exceptions.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">Built-in Exceptions</a><ul>
  86. <li><a class="reference internal" href="#exception-context">Exception context</a></li>
  87. <li><a class="reference internal" href="#inheriting-from-built-in-exceptions">Inheriting from built-in exceptions</a></li>
  88. <li><a class="reference internal" href="#base-classes">Base classes</a></li>
  89. <li><a class="reference internal" href="#concrete-exceptions">Concrete exceptions</a><ul>
  90. <li><a class="reference internal" href="#os-exceptions">OS exceptions</a></li>
  91. </ul>
  92. </li>
  93. <li><a class="reference internal" href="#warnings">Warnings</a></li>
  94. <li><a class="reference internal" href="#exception-groups">Exception groups</a></li>
  95. <li><a class="reference internal" href="#exception-hierarchy">Exception hierarchy</a></li>
  96. </ul>
  97. </li>
  98. </ul>
  99. </div>
  100. <div>
  101. <h4>Previous topic</h4>
  102. <p class="topless"><a href="stdtypes.html"
  103. title="previous chapter">Built-in Types</a></p>
  104. </div>
  105. <div>
  106. <h4>Next topic</h4>
  107. <p class="topless"><a href="text.html"
  108. title="next chapter">Text Processing Services</a></p>
  109. </div>
  110. <div role="note" aria-label="source link">
  111. <h3>This Page</h3>
  112. <ul class="this-page-menu">
  113. <li><a href="../bugs.html">Report a Bug</a></li>
  114. <li>
  115. <a href="https://github.com/python/cpython/blob/main/Doc/library/exceptions.rst"
  116. rel="nofollow">Show Source
  117. </a>
  118. </li>
  119. </ul>
  120. </div>
  121. </nav>
  122. </div>
  123. </div>
  124. <div class="related" role="navigation" aria-label="related navigation">
  125. <h3>Navigation</h3>
  126. <ul>
  127. <li class="right" style="margin-right: 10px">
  128. <a href="../genindex.html" title="General Index"
  129. accesskey="I">index</a></li>
  130. <li class="right" >
  131. <a href="../py-modindex.html" title="Python Module Index"
  132. >modules</a> |</li>
  133. <li class="right" >
  134. <a href="text.html" title="Text Processing Services"
  135. accesskey="N">next</a> |</li>
  136. <li class="right" >
  137. <a href="stdtypes.html" title="Built-in Types"
  138. accesskey="P">previous</a> |</li>
  139. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  140. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  141. <li class="switchers">
  142. <div class="language_switcher_placeholder"></div>
  143. <div class="version_switcher_placeholder"></div>
  144. </li>
  145. <li>
  146. </li>
  147. <li id="cpython-language-and-version">
  148. <a href="../index.html">3.12.3 Documentation</a> &#187;
  149. </li>
  150. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Standard Library</a> &#187;</li>
  151. <li class="nav-item nav-item-this"><a href="">Built-in Exceptions</a></li>
  152. <li class="right">
  153. <div class="inline-search" role="search">
  154. <form class="inline-search" action="../search.html" method="get">
  155. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  156. <input type="submit" value="Go" />
  157. </form>
  158. </div>
  159. |
  160. </li>
  161. <li class="right">
  162. <label class="theme-selector-label">
  163. Theme
  164. <select class="theme-selector" oninput="activateTheme(this.value)">
  165. <option value="auto" selected>Auto</option>
  166. <option value="light">Light</option>
  167. <option value="dark">Dark</option>
  168. </select>
  169. </label> |</li>
  170. </ul>
  171. </div>
  172. <div class="document">
  173. <div class="documentwrapper">
  174. <div class="bodywrapper">
  175. <div class="body" role="main">
  176. <section id="built-in-exceptions">
  177. <span id="bltin-exceptions"></span><h1>Built-in Exceptions<a class="headerlink" href="#built-in-exceptions" title="Link to this heading">¶</a></h1>
  178. <p id="index-0">In Python, all exceptions must be instances of a class that derives from
  179. <a class="reference internal" href="#BaseException" title="BaseException"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseException</span></code></a>. In a <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> statement with an <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a>
  180. clause that mentions a particular class, that clause also handles any exception
  181. classes derived from that class (but not exception classes from which <em>it</em> is
  182. derived). Two exception classes that are not related via subclassing are never
  183. equivalent, even if they have the same name.</p>
  184. <p id="index-1">The built-in exceptions listed in this chapter can be generated by the interpreter or
  185. built-in functions. Except where mentioned, they have an “associated value”
  186. indicating the detailed cause of the error. This may be a string or a tuple of
  187. several items of information (e.g., an error code and a string explaining the
  188. code). The associated value is usually passed as arguments to the exception
  189. class’s constructor.</p>
  190. <p>User code can raise built-in exceptions. This can be used to test an exception
  191. handler or to report an error condition “just like” the situation in which the
  192. interpreter raises the same exception; but beware that there is nothing to
  193. prevent user code from raising an inappropriate error.</p>
  194. <p>The built-in exception classes can be subclassed to define new exceptions;
  195. programmers are encouraged to derive new exceptions from the <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a>
  196. class or one of its subclasses, and not from <a class="reference internal" href="#BaseException" title="BaseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseException</span></code></a>. More
  197. information on defining exceptions is available in the Python Tutorial under
  198. <a class="reference internal" href="../tutorial/errors.html#tut-userexceptions"><span class="std std-ref">User-defined Exceptions</span></a>.</p>
  199. <section id="exception-context">
  200. <h2>Exception context<a class="headerlink" href="#exception-context" title="Link to this heading">¶</a></h2>
  201. <p id="index-2">Three attributes on exception objects provide information about the context in
  202. which the exception was raised:</p>
  203. <dl class="py attribute">
  204. <dt class="sig sig-object py" id="BaseException.__context__">
  205. <span class="sig-prename descclassname"><span class="pre">BaseException.</span></span><span class="sig-name descname"><span class="pre">__context__</span></span><a class="headerlink" href="#BaseException.__context__" title="Link to this definition">¶</a></dt>
  206. <dt class="sig sig-object py" id="BaseException.__cause__">
  207. <span class="sig-prename descclassname"><span class="pre">BaseException.</span></span><span class="sig-name descname"><span class="pre">__cause__</span></span><a class="headerlink" href="#BaseException.__cause__" title="Link to this definition">¶</a></dt>
  208. <dt class="sig sig-object py" id="BaseException.__suppress_context__">
  209. <span class="sig-prename descclassname"><span class="pre">BaseException.</span></span><span class="sig-name descname"><span class="pre">__suppress_context__</span></span><a class="headerlink" href="#BaseException.__suppress_context__" title="Link to this definition">¶</a></dt>
  210. <dd><p>When raising a new exception while another exception
  211. is already being handled, the new exception’s
  212. <code class="xref py py-attr docutils literal notranslate"><span class="pre">__context__</span></code> attribute is automatically set to the handled
  213. exception. An exception may be handled when an <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> or
  214. <a class="reference internal" href="../reference/compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> clause, or a <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement, is used.</p>
  215. <p>This implicit exception context can be
  216. supplemented with an explicit cause by using <code class="xref std std-keyword docutils literal notranslate"><span class="pre">from</span></code> with
  217. <a class="reference internal" href="../reference/simple_stmts.html#raise"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">raise</span></code></a>:</p>
  218. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">raise</span> <span class="n">new_exc</span> <span class="kn">from</span> <span class="nn">original_exc</span>
  219. </pre></div>
  220. </div>
  221. <p>The expression following <a class="reference internal" href="../reference/simple_stmts.html#raise"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">from</span></code></a> must be an exception or <code class="docutils literal notranslate"><span class="pre">None</span></code>. It
  222. will be set as <code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code> on the raised exception. Setting
  223. <code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code> also implicitly sets the <code class="xref py py-attr docutils literal notranslate"><span class="pre">__suppress_context__</span></code>
  224. attribute to <code class="docutils literal notranslate"><span class="pre">True</span></code>, so that using <code class="docutils literal notranslate"><span class="pre">raise</span> <span class="pre">new_exc</span> <span class="pre">from</span> <span class="pre">None</span></code>
  225. effectively replaces the old exception with the new one for display
  226. purposes (e.g. converting <a class="reference internal" href="#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> to <a class="reference internal" href="#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a>), while
  227. leaving the old exception available in <code class="xref py py-attr docutils literal notranslate"><span class="pre">__context__</span></code> for introspection
  228. when debugging.</p>
  229. <p>The default traceback display code shows these chained exceptions in
  230. addition to the traceback for the exception itself. An explicitly chained
  231. exception in <code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code> is always shown when present. An implicitly
  232. chained exception in <code class="xref py py-attr docutils literal notranslate"><span class="pre">__context__</span></code> is shown only if <code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code>
  233. is <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">__suppress_context__</span></code> is false.</p>
  234. <p>In either case, the exception itself is always shown after any chained
  235. exceptions so that the final line of the traceback always shows the last
  236. exception that was raised.</p>
  237. </dd></dl>
  238. </section>
  239. <section id="inheriting-from-built-in-exceptions">
  240. <h2>Inheriting from built-in exceptions<a class="headerlink" href="#inheriting-from-built-in-exceptions" title="Link to this heading">¶</a></h2>
  241. <p>User code can create subclasses that inherit from an exception type.
  242. It’s recommended to only subclass one exception type at a time to avoid
  243. any possible conflicts between how the bases handle the <code class="docutils literal notranslate"><span class="pre">args</span></code>
  244. attribute, as well as due to possible memory layout incompatibilities.</p>
  245. <div class="impl-detail compound">
  246. <p><strong>CPython implementation detail:</strong> Most built-in exceptions are implemented in C for efficiency, see:
  247. <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Objects/exceptions.c">Objects/exceptions.c</a>. Some have custom memory layouts
  248. which makes it impossible to create a subclass that inherits from
  249. multiple exception types. The memory layout of a type is an implementation
  250. detail and might change between Python versions, leading to new
  251. conflicts in the future. Therefore, it’s recommended to avoid
  252. subclassing multiple exception types altogether.</p>
  253. </div>
  254. </section>
  255. <section id="base-classes">
  256. <h2>Base classes<a class="headerlink" href="#base-classes" title="Link to this heading">¶</a></h2>
  257. <p>The following exceptions are used mostly as base classes for other exceptions.</p>
  258. <dl class="py exception">
  259. <dt class="sig sig-object py" id="BaseException">
  260. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">BaseException</span></span><a class="headerlink" href="#BaseException" title="Link to this definition">¶</a></dt>
  261. <dd><p>The base class for all built-in exceptions. It is not meant to be directly
  262. inherited by user-defined classes (for that, use <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a>). If
  263. <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> is called on an instance of this class, the representation of
  264. the argument(s) to the instance are returned, or the empty string when
  265. there were no arguments.</p>
  266. <dl class="py attribute">
  267. <dt class="sig sig-object py" id="BaseException.args">
  268. <span class="sig-name descname"><span class="pre">args</span></span><a class="headerlink" href="#BaseException.args" title="Link to this definition">¶</a></dt>
  269. <dd><p>The tuple of arguments given to the exception constructor. Some built-in
  270. exceptions (like <a class="reference internal" href="#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>) expect a certain number of arguments and
  271. assign a special meaning to the elements of this tuple, while others are
  272. usually called only with a single string giving an error message.</p>
  273. </dd></dl>
  274. <dl class="py method">
  275. <dt class="sig sig-object py" id="BaseException.with_traceback">
  276. <span class="sig-name descname"><span class="pre">with_traceback</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tb</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#BaseException.with_traceback" title="Link to this definition">¶</a></dt>
  277. <dd><p>This method sets <em>tb</em> as the new traceback for the exception and returns
  278. the exception object. It was more commonly used before the exception
  279. chaining features of <span class="target" id="index-3"></span><a class="pep reference external" href="https://peps.python.org/pep-3134/"><strong>PEP 3134</strong></a> became available. The following example
  280. shows how we can convert an instance of <code class="docutils literal notranslate"><span class="pre">SomeException</span></code> into an
  281. instance of <code class="docutils literal notranslate"><span class="pre">OtherException</span></code> while preserving the traceback. Once
  282. raised, the current frame is pushed onto the traceback of the
  283. <code class="docutils literal notranslate"><span class="pre">OtherException</span></code>, as would have happened to the traceback of the
  284. original <code class="docutils literal notranslate"><span class="pre">SomeException</span></code> had we allowed it to propagate to the caller.</p>
  285. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
  286. <span class="o">...</span>
  287. <span class="k">except</span> <span class="n">SomeException</span><span class="p">:</span>
  288. <span class="n">tb</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">exception</span><span class="p">()</span><span class="o">.</span><span class="n">__traceback__</span>
  289. <span class="k">raise</span> <span class="n">OtherException</span><span class="p">(</span><span class="o">...</span><span class="p">)</span><span class="o">.</span><span class="n">with_traceback</span><span class="p">(</span><span class="n">tb</span><span class="p">)</span>
  290. </pre></div>
  291. </div>
  292. </dd></dl>
  293. <dl class="py attribute">
  294. <dt class="sig sig-object py" id="BaseException.__traceback__">
  295. <span class="sig-name descname"><span class="pre">__traceback__</span></span><a class="headerlink" href="#BaseException.__traceback__" title="Link to this definition">¶</a></dt>
  296. <dd><p>A writable field that holds the
  297. <a class="reference internal" href="../reference/datamodel.html#traceback-objects"><span class="std std-ref">traceback object</span></a> associated with this
  298. exception. See also: <a class="reference internal" href="../reference/simple_stmts.html#raise"><span class="std std-ref">The raise statement</span></a>.</p>
  299. </dd></dl>
  300. <dl class="py method">
  301. <dt class="sig sig-object py" id="BaseException.add_note">
  302. <span class="sig-name descname"><span class="pre">add_note</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">note</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#BaseException.add_note" title="Link to this definition">¶</a></dt>
  303. <dd><p>Add the string <code class="docutils literal notranslate"><span class="pre">note</span></code> to the exception’s notes which appear in the standard
  304. traceback after the exception string. A <a class="reference internal" href="#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised if <code class="docutils literal notranslate"><span class="pre">note</span></code>
  305. is not a string.</p>
  306. <div class="versionadded">
  307. <p><span class="versionmodified added">New in version 3.11.</span></p>
  308. </div>
  309. </dd></dl>
  310. <dl class="py attribute">
  311. <dt class="sig sig-object py" id="BaseException.__notes__">
  312. <span class="sig-name descname"><span class="pre">__notes__</span></span><a class="headerlink" href="#BaseException.__notes__" title="Link to this definition">¶</a></dt>
  313. <dd><p>A list of the notes of this exception, which were added with <a class="reference internal" href="#BaseException.add_note" title="BaseException.add_note"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add_note()</span></code></a>.
  314. This attribute is created when <a class="reference internal" href="#BaseException.add_note" title="BaseException.add_note"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add_note()</span></code></a> is called.</p>
  315. <div class="versionadded">
  316. <p><span class="versionmodified added">New in version 3.11.</span></p>
  317. </div>
  318. </dd></dl>
  319. </dd></dl>
  320. <dl class="py exception">
  321. <dt class="sig sig-object py" id="Exception">
  322. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">Exception</span></span><a class="headerlink" href="#Exception" title="Link to this definition">¶</a></dt>
  323. <dd><p>All built-in, non-system-exiting exceptions are derived from this class. All
  324. user-defined exceptions should also be derived from this class.</p>
  325. </dd></dl>
  326. <dl class="py exception">
  327. <dt class="sig sig-object py" id="ArithmeticError">
  328. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">ArithmeticError</span></span><a class="headerlink" href="#ArithmeticError" title="Link to this definition">¶</a></dt>
  329. <dd><p>The base class for those built-in exceptions that are raised for various
  330. arithmetic errors: <a class="reference internal" href="#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>, <a class="reference internal" href="#ZeroDivisionError" title="ZeroDivisionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZeroDivisionError</span></code></a>,
  331. <a class="reference internal" href="#FloatingPointError" title="FloatingPointError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FloatingPointError</span></code></a>.</p>
  332. </dd></dl>
  333. <dl class="py exception">
  334. <dt class="sig sig-object py" id="BufferError">
  335. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">BufferError</span></span><a class="headerlink" href="#BufferError" title="Link to this definition">¶</a></dt>
  336. <dd><p>Raised when a <a class="reference internal" href="../c-api/buffer.html#bufferobjects"><span class="std std-ref">buffer</span></a> related operation cannot be
  337. performed.</p>
  338. </dd></dl>
  339. <dl class="py exception">
  340. <dt class="sig sig-object py" id="LookupError">
  341. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">LookupError</span></span><a class="headerlink" href="#LookupError" title="Link to this definition">¶</a></dt>
  342. <dd><p>The base class for the exceptions that are raised when a key or index used on
  343. a mapping or sequence is invalid: <a class="reference internal" href="#IndexError" title="IndexError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndexError</span></code></a>, <a class="reference internal" href="#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a>. This
  344. can be raised directly by <a class="reference internal" href="codecs.html#codecs.lookup" title="codecs.lookup"><code class="xref py py-func docutils literal notranslate"><span class="pre">codecs.lookup()</span></code></a>.</p>
  345. </dd></dl>
  346. </section>
  347. <section id="concrete-exceptions">
  348. <h2>Concrete exceptions<a class="headerlink" href="#concrete-exceptions" title="Link to this heading">¶</a></h2>
  349. <p>The following exceptions are the exceptions that are usually raised.</p>
  350. <dl class="py exception">
  351. <dt class="sig sig-object py" id="AssertionError">
  352. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">AssertionError</span></span><a class="headerlink" href="#AssertionError" title="Link to this definition">¶</a></dt>
  353. <dd><p id="index-4">Raised when an <a class="reference internal" href="../reference/simple_stmts.html#assert"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">assert</span></code></a> statement fails.</p>
  354. </dd></dl>
  355. <dl class="py exception">
  356. <dt class="sig sig-object py" id="AttributeError">
  357. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">AttributeError</span></span><a class="headerlink" href="#AttributeError" title="Link to this definition">¶</a></dt>
  358. <dd><p>Raised when an attribute reference (see <a class="reference internal" href="../reference/expressions.html#attribute-references"><span class="std std-ref">Attribute references</span></a>) or
  359. assignment fails. (When an object does not support attribute references or
  360. attribute assignments at all, <a class="reference internal" href="#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised.)</p>
  361. <p>The <code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">obj</span></code> attributes can be set using keyword-only
  362. arguments to the constructor. When set they represent the name of the attribute
  363. that was attempted to be accessed and the object that was accessed for said
  364. attribute, respectively.</p>
  365. <div class="versionchanged">
  366. <p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">obj</span></code> attributes.</p>
  367. </div>
  368. </dd></dl>
  369. <dl class="py exception">
  370. <dt class="sig sig-object py" id="EOFError">
  371. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">EOFError</span></span><a class="headerlink" href="#EOFError" title="Link to this definition">¶</a></dt>
  372. <dd><p>Raised when the <a class="reference internal" href="functions.html#input" title="input"><code class="xref py py-func docutils literal notranslate"><span class="pre">input()</span></code></a> function hits an end-of-file condition (EOF)
  373. without reading any data. (N.B.: the <code class="xref py py-meth docutils literal notranslate"><span class="pre">io.IOBase.read()</span></code> and
  374. <a class="reference internal" href="io.html#io.IOBase.readline" title="io.IOBase.readline"><code class="xref py py-meth docutils literal notranslate"><span class="pre">io.IOBase.readline()</span></code></a> methods return an empty string when they hit EOF.)</p>
  375. </dd></dl>
  376. <dl class="py exception">
  377. <dt class="sig sig-object py" id="FloatingPointError">
  378. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">FloatingPointError</span></span><a class="headerlink" href="#FloatingPointError" title="Link to this definition">¶</a></dt>
  379. <dd><p>Not currently used.</p>
  380. </dd></dl>
  381. <dl class="py exception">
  382. <dt class="sig sig-object py" id="GeneratorExit">
  383. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">GeneratorExit</span></span><a class="headerlink" href="#GeneratorExit" title="Link to this definition">¶</a></dt>
  384. <dd><p>Raised when a <a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generator</span></a> or <a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">coroutine</span></a> is closed;
  385. see <a class="reference internal" href="../reference/expressions.html#generator.close" title="generator.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">generator.close()</span></code></a> and <a class="reference internal" href="../reference/datamodel.html#coroutine.close" title="coroutine.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">coroutine.close()</span></code></a>. It
  386. directly inherits from <a class="reference internal" href="#BaseException" title="BaseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseException</span></code></a> instead of <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a> since
  387. it is technically not an error.</p>
  388. </dd></dl>
  389. <dl class="py exception">
  390. <dt class="sig sig-object py" id="ImportError">
  391. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">ImportError</span></span><a class="headerlink" href="#ImportError" title="Link to this definition">¶</a></dt>
  392. <dd><p>Raised when the <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement has troubles trying to
  393. load a module. Also raised when the “from list” in <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">...</span> <span class="pre">import</span></code>
  394. has a name that cannot be found.</p>
  395. <p>The optional <em>name</em> and <em>path</em> keyword-only arguments
  396. set the corresponding attributes:</p>
  397. <dl class="py attribute">
  398. <dt class="sig sig-object py" id="ImportError.name">
  399. <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#ImportError.name" title="Link to this definition">¶</a></dt>
  400. <dd><p>The name of the module that was attempted to be imported.</p>
  401. </dd></dl>
  402. <dl class="py attribute">
  403. <dt class="sig sig-object py" id="ImportError.path">
  404. <span class="sig-name descname"><span class="pre">path</span></span><a class="headerlink" href="#ImportError.path" title="Link to this definition">¶</a></dt>
  405. <dd><p>The path to any file which triggered the exception.</p>
  406. </dd></dl>
  407. <div class="versionchanged">
  408. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <a class="reference internal" href="#ImportError.name" title="ImportError.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a> and <a class="reference internal" href="#ImportError.path" title="ImportError.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a> attributes.</p>
  409. </div>
  410. </dd></dl>
  411. <dl class="py exception">
  412. <dt class="sig sig-object py" id="ModuleNotFoundError">
  413. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">ModuleNotFoundError</span></span><a class="headerlink" href="#ModuleNotFoundError" title="Link to this definition">¶</a></dt>
  414. <dd><p>A subclass of <a class="reference internal" href="#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> which is raised by <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a>
  415. when a module could not be located. It is also raised when <code class="docutils literal notranslate"><span class="pre">None</span></code>
  416. is found in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>.</p>
  417. <div class="versionadded">
  418. <p><span class="versionmodified added">New in version 3.6.</span></p>
  419. </div>
  420. </dd></dl>
  421. <dl class="py exception">
  422. <dt class="sig sig-object py" id="IndexError">
  423. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">IndexError</span></span><a class="headerlink" href="#IndexError" title="Link to this definition">¶</a></dt>
  424. <dd><p>Raised when a sequence subscript is out of range. (Slice indices are
  425. silently truncated to fall in the allowed range; if an index is not an
  426. integer, <a class="reference internal" href="#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised.)</p>
  427. </dd></dl>
  428. <dl class="py exception">
  429. <dt class="sig sig-object py" id="KeyError">
  430. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">KeyError</span></span><a class="headerlink" href="#KeyError" title="Link to this definition">¶</a></dt>
  431. <dd><p>Raised when a mapping (dictionary) key is not found in the set of existing keys.</p>
  432. </dd></dl>
  433. <dl class="py exception">
  434. <dt class="sig sig-object py" id="KeyboardInterrupt">
  435. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">KeyboardInterrupt</span></span><a class="headerlink" href="#KeyboardInterrupt" title="Link to this definition">¶</a></dt>
  436. <dd><p>Raised when the user hits the interrupt key (normally <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Control</kbd>-<kbd class="kbd docutils literal notranslate">C</kbd></kbd> or
  437. <kbd class="kbd docutils literal notranslate">Delete</kbd>). During execution, a check for interrupts is made
  438. regularly. The exception inherits from <a class="reference internal" href="#BaseException" title="BaseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseException</span></code></a> so as to not be
  439. accidentally caught by code that catches <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a> and thus prevent
  440. the interpreter from exiting.</p>
  441. <div class="admonition note">
  442. <p class="admonition-title">Note</p>
  443. <p>Catching a <a class="reference internal" href="#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a> requires special consideration.
  444. Because it can be raised at unpredictable points, it may, in some
  445. circumstances, leave the running program in an inconsistent state. It is
  446. generally best to allow <a class="reference internal" href="#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a> to end the program as
  447. quickly as possible or avoid raising it entirely. (See
  448. <a class="reference internal" href="signal.html#handlers-and-exceptions"><span class="std std-ref">Note on Signal Handlers and Exceptions</span></a>.)</p>
  449. </div>
  450. </dd></dl>
  451. <dl class="py exception">
  452. <dt class="sig sig-object py" id="MemoryError">
  453. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">MemoryError</span></span><a class="headerlink" href="#MemoryError" title="Link to this definition">¶</a></dt>
  454. <dd><p>Raised when an operation runs out of memory but the situation may still be
  455. rescued (by deleting some objects). The associated value is a string indicating
  456. what kind of (internal) operation ran out of memory. Note that because of the
  457. underlying memory management architecture (C’s <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code> function), the
  458. interpreter may not always be able to completely recover from this situation; it
  459. nevertheless raises an exception so that a stack traceback can be printed, in
  460. case a run-away program was the cause.</p>
  461. </dd></dl>
  462. <dl class="py exception">
  463. <dt class="sig sig-object py" id="NameError">
  464. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">NameError</span></span><a class="headerlink" href="#NameError" title="Link to this definition">¶</a></dt>
  465. <dd><p>Raised when a local or global name is not found. This applies only to
  466. unqualified names. The associated value is an error message that includes the
  467. name that could not be found.</p>
  468. <p>The <code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code> attribute can be set using a keyword-only argument to the
  469. constructor. When set it represent the name of the variable that was attempted
  470. to be accessed.</p>
  471. <div class="versionchanged">
  472. <p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code> attribute.</p>
  473. </div>
  474. </dd></dl>
  475. <dl class="py exception">
  476. <dt class="sig sig-object py" id="NotImplementedError">
  477. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">NotImplementedError</span></span><a class="headerlink" href="#NotImplementedError" title="Link to this definition">¶</a></dt>
  478. <dd><p>This exception is derived from <a class="reference internal" href="#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>. In user defined base
  479. classes, abstract methods should raise this exception when they require
  480. derived classes to override the method, or while the class is being
  481. developed to indicate that the real implementation still needs to be added.</p>
  482. <div class="admonition note">
  483. <p class="admonition-title">Note</p>
  484. <p>It should not be used to indicate that an operator or method is not
  485. meant to be supported at all – in that case either leave the operator /
  486. method undefined or, if a subclass, set it to <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-data docutils literal notranslate"><span class="pre">None</span></code></a>.</p>
  487. </div>
  488. <div class="admonition note">
  489. <p class="admonition-title">Note</p>
  490. <p><code class="docutils literal notranslate"><span class="pre">NotImplementedError</span></code> and <a class="reference internal" href="constants.html#NotImplemented" title="NotImplemented"><code class="xref py py-data docutils literal notranslate"><span class="pre">NotImplemented</span></code></a> are not interchangeable,
  491. even though they have similar names and purposes. See
  492. <code class="xref py py-data docutils literal notranslate"><span class="pre">NotImplemented</span></code> for details on when to use it.</p>
  493. </div>
  494. </dd></dl>
  495. <dl class="py exception">
  496. <dt class="sig sig-object py" id="OSError">
  497. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">OSError</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">arg</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#OSError" title="Link to this definition">¶</a></dt>
  498. <dt class="sig sig-object py">
  499. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">OSError</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">errno</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">strerror</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">filename</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">winerror</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">filename2</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span></dt>
  500. <dd><p id="index-5">This exception is raised when a system function returns a system-related
  501. error, including I/O failures such as “file not found” or “disk full”
  502. (not for illegal argument types or other incidental errors).</p>
  503. <p>The second form of the constructor sets the corresponding attributes,
  504. described below. The attributes default to <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> if not
  505. specified. For backwards compatibility, if three arguments are passed,
  506. the <a class="reference internal" href="#BaseException.args" title="BaseException.args"><code class="xref py py-attr docutils literal notranslate"><span class="pre">args</span></code></a> attribute contains only a 2-tuple
  507. of the first two constructor arguments.</p>
  508. <p>The constructor often actually returns a subclass of <a class="reference internal" href="#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>, as
  509. described in <a class="reference internal" href="#os-exceptions">OS exceptions</a> below. The particular subclass depends on
  510. the final <a class="reference internal" href="#OSError.errno" title="OSError.errno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">errno</span></code></a> value. This behaviour only occurs when
  511. constructing <a class="reference internal" href="#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> directly or via an alias, and is not
  512. inherited when subclassing.</p>
  513. <dl class="py attribute">
  514. <dt class="sig sig-object py" id="OSError.errno">
  515. <span class="sig-name descname"><span class="pre">errno</span></span><a class="headerlink" href="#OSError.errno" title="Link to this definition">¶</a></dt>
  516. <dd><p>A numeric error code from the C variable <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code>.</p>
  517. </dd></dl>
  518. <dl class="py attribute">
  519. <dt class="sig sig-object py" id="OSError.winerror">
  520. <span class="sig-name descname"><span class="pre">winerror</span></span><a class="headerlink" href="#OSError.winerror" title="Link to this definition">¶</a></dt>
  521. <dd><p>Under Windows, this gives you the native
  522. Windows error code. The <a class="reference internal" href="#OSError.errno" title="OSError.errno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">errno</span></code></a> attribute is then an approximate
  523. translation, in POSIX terms, of that native error code.</p>
  524. <p>Under Windows, if the <em>winerror</em> constructor argument is an integer,
  525. the <a class="reference internal" href="#OSError.errno" title="OSError.errno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">errno</span></code></a> attribute is determined from the Windows error code,
  526. and the <em>errno</em> argument is ignored. On other platforms, the
  527. <em>winerror</em> argument is ignored, and the <a class="reference internal" href="#OSError.winerror" title="OSError.winerror"><code class="xref py py-attr docutils literal notranslate"><span class="pre">winerror</span></code></a> attribute
  528. does not exist.</p>
  529. </dd></dl>
  530. <dl class="py attribute">
  531. <dt class="sig sig-object py" id="OSError.strerror">
  532. <span class="sig-name descname"><span class="pre">strerror</span></span><a class="headerlink" href="#OSError.strerror" title="Link to this definition">¶</a></dt>
  533. <dd><p>The corresponding error message, as provided by
  534. the operating system. It is formatted by the C
  535. functions <code class="xref c c-func docutils literal notranslate"><span class="pre">perror()</span></code> under POSIX, and <code class="xref c c-func docutils literal notranslate"><span class="pre">FormatMessage()</span></code>
  536. under Windows.</p>
  537. </dd></dl>
  538. <dl class="py attribute">
  539. <dt class="sig sig-object py" id="OSError.filename">
  540. <span class="sig-name descname"><span class="pre">filename</span></span><a class="headerlink" href="#OSError.filename" title="Link to this definition">¶</a></dt>
  541. <dt class="sig sig-object py" id="OSError.filename2">
  542. <span class="sig-name descname"><span class="pre">filename2</span></span><a class="headerlink" href="#OSError.filename2" title="Link to this definition">¶</a></dt>
  543. <dd><p>For exceptions that involve a file system path (such as <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> or
  544. <a class="reference internal" href="os.html#os.unlink" title="os.unlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.unlink()</span></code></a>), <a class="reference internal" href="#OSError.filename" title="OSError.filename"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filename</span></code></a> is the file name passed to the function.
  545. For functions that involve two file system paths (such as
  546. <a class="reference internal" href="os.html#os.rename" title="os.rename"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.rename()</span></code></a>), <a class="reference internal" href="#OSError.filename2" title="OSError.filename2"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filename2</span></code></a> corresponds to the second
  547. file name passed to the function.</p>
  548. </dd></dl>
  549. <div class="versionchanged">
  550. <p><span class="versionmodified changed">Changed in version 3.3: </span><a class="reference internal" href="#EnvironmentError" title="EnvironmentError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EnvironmentError</span></code></a>, <a class="reference internal" href="#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a>, <a class="reference internal" href="#WindowsError" title="WindowsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">WindowsError</span></code></a>,
  551. <a class="reference internal" href="socket.html#socket.error" title="socket.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">socket.error</span></code></a>, <a class="reference internal" href="select.html#select.error" title="select.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">select.error</span></code></a> and
  552. <code class="xref py py-exc docutils literal notranslate"><span class="pre">mmap.error</span></code> have been merged into <a class="reference internal" href="#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>, and the
  553. constructor may return a subclass.</p>
  554. </div>
  555. <div class="versionchanged">
  556. <p><span class="versionmodified changed">Changed in version 3.4: </span>The <a class="reference internal" href="#OSError.filename" title="OSError.filename"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filename</span></code></a> attribute is now the original file name passed to
  557. the function, instead of the name encoded to or decoded from the
  558. <a class="reference internal" href="../glossary.html#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a>. Also, the <em>filename2</em>
  559. constructor argument and attribute was added.</p>
  560. </div>
  561. </dd></dl>
  562. <dl class="py exception">
  563. <dt class="sig sig-object py" id="OverflowError">
  564. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">OverflowError</span></span><a class="headerlink" href="#OverflowError" title="Link to this definition">¶</a></dt>
  565. <dd><p>Raised when the result of an arithmetic operation is too large to be
  566. represented. This cannot occur for integers (which would rather raise
  567. <a class="reference internal" href="#MemoryError" title="MemoryError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">MemoryError</span></code></a> than give up). However, for historical reasons,
  568. OverflowError is sometimes raised for integers that are outside a required
  569. range. Because of the lack of standardization of floating point exception
  570. handling in C, most floating point operations are not checked.</p>
  571. </dd></dl>
  572. <dl class="py exception">
  573. <dt class="sig sig-object py" id="RecursionError">
  574. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">RecursionError</span></span><a class="headerlink" href="#RecursionError" title="Link to this definition">¶</a></dt>
  575. <dd><p>This exception is derived from <a class="reference internal" href="#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>. It is raised when the
  576. interpreter detects that the maximum recursion depth (see
  577. <a class="reference internal" href="sys.html#sys.getrecursionlimit" title="sys.getrecursionlimit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getrecursionlimit()</span></code></a>) is exceeded.</p>
  578. <div class="versionadded">
  579. <p><span class="versionmodified added">New in version 3.5: </span>Previously, a plain <a class="reference internal" href="#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> was raised.</p>
  580. </div>
  581. </dd></dl>
  582. <dl class="py exception">
  583. <dt class="sig sig-object py" id="ReferenceError">
  584. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">ReferenceError</span></span><a class="headerlink" href="#ReferenceError" title="Link to this definition">¶</a></dt>
  585. <dd><p>This exception is raised when a weak reference proxy, created by the
  586. <a class="reference internal" href="weakref.html#weakref.proxy" title="weakref.proxy"><code class="xref py py-func docutils literal notranslate"><span class="pre">weakref.proxy()</span></code></a> function, is used to access an attribute of the referent
  587. after it has been garbage collected. For more information on weak references,
  588. see the <a class="reference internal" href="weakref.html#module-weakref" title="weakref: Support for weak references and weak dictionaries."><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code></a> module.</p>
  589. </dd></dl>
  590. <dl class="py exception">
  591. <dt class="sig sig-object py" id="RuntimeError">
  592. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">RuntimeError</span></span><a class="headerlink" href="#RuntimeError" title="Link to this definition">¶</a></dt>
  593. <dd><p>Raised when an error is detected that doesn’t fall in any of the other
  594. categories. The associated value is a string indicating what precisely went
  595. wrong.</p>
  596. </dd></dl>
  597. <dl class="py exception">
  598. <dt class="sig sig-object py" id="StopIteration">
  599. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">StopIteration</span></span><a class="headerlink" href="#StopIteration" title="Link to this definition">¶</a></dt>
  600. <dd><p>Raised by built-in function <a class="reference internal" href="functions.html#next" title="next"><code class="xref py py-func docutils literal notranslate"><span class="pre">next()</span></code></a> and an <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a>'s
  601. <a class="reference internal" href="stdtypes.html#iterator.__next__" title="iterator.__next__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code></a> method to signal that there are no further
  602. items produced by the iterator.</p>
  603. <dl class="py attribute">
  604. <dt class="sig sig-object py" id="StopIteration.value">
  605. <span class="sig-name descname"><span class="pre">value</span></span><a class="headerlink" href="#StopIteration.value" title="Link to this definition">¶</a></dt>
  606. <dd><p>The exception object has a single attribute <code class="xref py py-attr docutils literal notranslate"><span class="pre">value</span></code>, which is
  607. given as an argument when constructing the exception, and defaults
  608. to <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>.</p>
  609. </dd></dl>
  610. <p>When a <a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generator</span></a> or <a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">coroutine</span></a> function
  611. returns, a new <a class="reference internal" href="#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a> instance is
  612. raised, and the value returned by the function is used as the
  613. <a class="reference internal" href="#StopIteration.value" title="StopIteration.value"><code class="xref py py-attr docutils literal notranslate"><span class="pre">value</span></code></a> parameter to the constructor of the exception.</p>
  614. <p>If a generator code directly or indirectly raises <a class="reference internal" href="#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a>,
  615. it is converted into a <a class="reference internal" href="#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> (retaining the
  616. <a class="reference internal" href="#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a> as the new exception’s cause).</p>
  617. <div class="versionchanged">
  618. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added <code class="docutils literal notranslate"><span class="pre">value</span></code> attribute and the ability for generator functions to
  619. use it to return a value.</p>
  620. </div>
  621. <div class="versionchanged">
  622. <p><span class="versionmodified changed">Changed in version 3.5: </span>Introduced the RuntimeError transformation via
  623. <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">generator_stop</span></code>, see <span class="target" id="index-6"></span><a class="pep reference external" href="https://peps.python.org/pep-0479/"><strong>PEP 479</strong></a>.</p>
  624. </div>
  625. <div class="versionchanged">
  626. <p><span class="versionmodified changed">Changed in version 3.7: </span>Enable <span class="target" id="index-7"></span><a class="pep reference external" href="https://peps.python.org/pep-0479/"><strong>PEP 479</strong></a> for all code by default: a <a class="reference internal" href="#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a>
  627. error raised in a generator is transformed into a <a class="reference internal" href="#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>.</p>
  628. </div>
  629. </dd></dl>
  630. <dl class="py exception">
  631. <dt class="sig sig-object py" id="StopAsyncIteration">
  632. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">StopAsyncIteration</span></span><a class="headerlink" href="#StopAsyncIteration" title="Link to this definition">¶</a></dt>
  633. <dd><p>Must be raised by <a class="reference internal" href="../reference/datamodel.html#object.__anext__" title="object.__anext__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__anext__()</span></code></a> method of an
  634. <a class="reference internal" href="../glossary.html#term-asynchronous-iterator"><span class="xref std std-term">asynchronous iterator</span></a> object to stop the iteration.</p>
  635. <div class="versionadded">
  636. <p><span class="versionmodified added">New in version 3.5.</span></p>
  637. </div>
  638. </dd></dl>
  639. <dl class="py exception">
  640. <dt class="sig sig-object py" id="SyntaxError">
  641. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">SyntaxError</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">message</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">details</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#SyntaxError" title="Link to this definition">¶</a></dt>
  642. <dd><p>Raised when the parser encounters a syntax error. This may occur in an
  643. <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement, in a call to the built-in functions
  644. <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a>, <a class="reference internal" href="functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a>,
  645. or <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a>, or when reading the initial script or standard input
  646. (also interactively).</p>
  647. <p>The <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> of the exception instance returns only the error message.
  648. Details is a tuple whose members are also available as separate attributes.</p>
  649. <dl class="py attribute">
  650. <dt class="sig sig-object py" id="SyntaxError.filename">
  651. <span class="sig-name descname"><span class="pre">filename</span></span><a class="headerlink" href="#SyntaxError.filename" title="Link to this definition">¶</a></dt>
  652. <dd><p>The name of the file the syntax error occurred in.</p>
  653. </dd></dl>
  654. <dl class="py attribute">
  655. <dt class="sig sig-object py" id="SyntaxError.lineno">
  656. <span class="sig-name descname"><span class="pre">lineno</span></span><a class="headerlink" href="#SyntaxError.lineno" title="Link to this definition">¶</a></dt>
  657. <dd><p>Which line number in the file the error occurred in. This is
  658. 1-indexed: the first line in the file has a <code class="docutils literal notranslate"><span class="pre">lineno</span></code> of 1.</p>
  659. </dd></dl>
  660. <dl class="py attribute">
  661. <dt class="sig sig-object py" id="SyntaxError.offset">
  662. <span class="sig-name descname"><span class="pre">offset</span></span><a class="headerlink" href="#SyntaxError.offset" title="Link to this definition">¶</a></dt>
  663. <dd><p>The column in the line where the error occurred. This is
  664. 1-indexed: the first character in the line has an <code class="docutils literal notranslate"><span class="pre">offset</span></code> of 1.</p>
  665. </dd></dl>
  666. <dl class="py attribute">
  667. <dt class="sig sig-object py" id="SyntaxError.text">
  668. <span class="sig-name descname"><span class="pre">text</span></span><a class="headerlink" href="#SyntaxError.text" title="Link to this definition">¶</a></dt>
  669. <dd><p>The source code text involved in the error.</p>
  670. </dd></dl>
  671. <dl class="py attribute">
  672. <dt class="sig sig-object py" id="SyntaxError.end_lineno">
  673. <span class="sig-name descname"><span class="pre">end_lineno</span></span><a class="headerlink" href="#SyntaxError.end_lineno" title="Link to this definition">¶</a></dt>
  674. <dd><p>Which line number in the file the error occurred ends in. This is
  675. 1-indexed: the first line in the file has a <code class="docutils literal notranslate"><span class="pre">lineno</span></code> of 1.</p>
  676. </dd></dl>
  677. <dl class="py attribute">
  678. <dt class="sig sig-object py" id="SyntaxError.end_offset">
  679. <span class="sig-name descname"><span class="pre">end_offset</span></span><a class="headerlink" href="#SyntaxError.end_offset" title="Link to this definition">¶</a></dt>
  680. <dd><p>The column in the end line where the error occurred finishes. This is
  681. 1-indexed: the first character in the line has an <code class="docutils literal notranslate"><span class="pre">offset</span></code> of 1.</p>
  682. </dd></dl>
  683. <p>For errors in f-string fields, the message is prefixed by “f-string: ”
  684. and the offsets are offsets in a text constructed from the replacement
  685. expression. For example, compiling f’Bad {a b} field’ results in this
  686. args attribute: (‘f-string: …’, (‘’, 1, 2, ‘(a b)n’, 1, 5)).</p>
  687. <div class="versionchanged">
  688. <p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <a class="reference internal" href="#SyntaxError.end_lineno" title="SyntaxError.end_lineno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">end_lineno</span></code></a> and <a class="reference internal" href="#SyntaxError.end_offset" title="SyntaxError.end_offset"><code class="xref py py-attr docutils literal notranslate"><span class="pre">end_offset</span></code></a> attributes.</p>
  689. </div>
  690. </dd></dl>
  691. <dl class="py exception">
  692. <dt class="sig sig-object py" id="IndentationError">
  693. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">IndentationError</span></span><a class="headerlink" href="#IndentationError" title="Link to this definition">¶</a></dt>
  694. <dd><p>Base class for syntax errors related to incorrect indentation. This is a
  695. subclass of <a class="reference internal" href="#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a>.</p>
  696. </dd></dl>
  697. <dl class="py exception">
  698. <dt class="sig sig-object py" id="TabError">
  699. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">TabError</span></span><a class="headerlink" href="#TabError" title="Link to this definition">¶</a></dt>
  700. <dd><p>Raised when indentation contains an inconsistent use of tabs and spaces.
  701. This is a subclass of <a class="reference internal" href="#IndentationError" title="IndentationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndentationError</span></code></a>.</p>
  702. </dd></dl>
  703. <dl class="py exception">
  704. <dt class="sig sig-object py" id="SystemError">
  705. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">SystemError</span></span><a class="headerlink" href="#SystemError" title="Link to this definition">¶</a></dt>
  706. <dd><p>Raised when the interpreter finds an internal error, but the situation does not
  707. look so serious to cause it to abandon all hope. The associated value is a
  708. string indicating what went wrong (in low-level terms).</p>
  709. <p>You should report this to the author or maintainer of your Python interpreter.
  710. Be sure to report the version of the Python interpreter (<code class="docutils literal notranslate"><span class="pre">sys.version</span></code>; it is
  711. also printed at the start of an interactive Python session), the exact error
  712. message (the exception’s associated value) and if possible the source of the
  713. program that triggered the error.</p>
  714. </dd></dl>
  715. <dl class="py exception">
  716. <dt class="sig sig-object py" id="SystemExit">
  717. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">SystemExit</span></span><a class="headerlink" href="#SystemExit" title="Link to this definition">¶</a></dt>
  718. <dd><p>This exception is raised by the <a class="reference internal" href="sys.html#sys.exit" title="sys.exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exit()</span></code></a> function. It inherits from
  719. <a class="reference internal" href="#BaseException" title="BaseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseException</span></code></a> instead of <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a> so that it is not accidentally
  720. caught by code that catches <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a>. This allows the exception to
  721. properly propagate up and cause the interpreter to exit. When it is not
  722. handled, the Python interpreter exits; no stack traceback is printed. The
  723. constructor accepts the same optional argument passed to <a class="reference internal" href="sys.html#sys.exit" title="sys.exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exit()</span></code></a>.
  724. If the value is an integer, it specifies the system exit status (passed to
  725. C’s <code class="xref c c-func docutils literal notranslate"><span class="pre">exit()</span></code> function); if it is <code class="docutils literal notranslate"><span class="pre">None</span></code>, the exit status is zero; if
  726. it has another type (such as a string), the object’s value is printed and
  727. the exit status is one.</p>
  728. <p>A call to <a class="reference internal" href="sys.html#sys.exit" title="sys.exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exit()</span></code></a> is translated into an exception so that clean-up
  729. handlers (<a class="reference internal" href="../reference/compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> clauses of <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> statements) can be
  730. executed, and so that a debugger can execute a script without running the risk
  731. of losing control. The <a class="reference internal" href="os.html#os._exit" title="os._exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">os._exit()</span></code></a> function can be used if it is
  732. absolutely positively necessary to exit immediately (for example, in the child
  733. process after a call to <a class="reference internal" href="os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a>).</p>
  734. <dl class="py attribute">
  735. <dt class="sig sig-object py" id="SystemExit.code">
  736. <span class="sig-name descname"><span class="pre">code</span></span><a class="headerlink" href="#SystemExit.code" title="Link to this definition">¶</a></dt>
  737. <dd><p>The exit status or error message that is passed to the constructor.
  738. (Defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code>.)</p>
  739. </dd></dl>
  740. </dd></dl>
  741. <dl class="py exception">
  742. <dt class="sig sig-object py" id="TypeError">
  743. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">TypeError</span></span><a class="headerlink" href="#TypeError" title="Link to this definition">¶</a></dt>
  744. <dd><p>Raised when an operation or function is applied to an object of inappropriate
  745. type. The associated value is a string giving details about the type mismatch.</p>
  746. <p>This exception may be raised by user code to indicate that an attempted
  747. operation on an object is not supported, and is not meant to be. If an object
  748. is meant to support a given operation but has not yet provided an
  749. implementation, <a class="reference internal" href="#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> is the proper exception to raise.</p>
  750. <p>Passing arguments of the wrong type (e.g. passing a <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> when an
  751. <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> is expected) should result in a <a class="reference internal" href="#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>, but passing
  752. arguments with the wrong value (e.g. a number outside expected boundaries)
  753. should result in a <a class="reference internal" href="#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>
  754. </dd></dl>
  755. <dl class="py exception">
  756. <dt class="sig sig-object py" id="UnboundLocalError">
  757. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">UnboundLocalError</span></span><a class="headerlink" href="#UnboundLocalError" title="Link to this definition">¶</a></dt>
  758. <dd><p>Raised when a reference is made to a local variable in a function or method, but
  759. no value has been bound to that variable. This is a subclass of
  760. <a class="reference internal" href="#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a>.</p>
  761. </dd></dl>
  762. <dl class="py exception">
  763. <dt class="sig sig-object py" id="UnicodeError">
  764. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">UnicodeError</span></span><a class="headerlink" href="#UnicodeError" title="Link to this definition">¶</a></dt>
  765. <dd><p>Raised when a Unicode-related encoding or decoding error occurs. It is a
  766. subclass of <a class="reference internal" href="#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>
  767. <p><a class="reference internal" href="#UnicodeError" title="UnicodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeError</span></code></a> has attributes that describe the encoding or decoding
  768. error. For example, <code class="docutils literal notranslate"><span class="pre">err.object[err.start:err.end]</span></code> gives the particular
  769. invalid input that the codec failed on.</p>
  770. <dl class="py attribute">
  771. <dt class="sig sig-object py" id="UnicodeError.encoding">
  772. <span class="sig-name descname"><span class="pre">encoding</span></span><a class="headerlink" href="#UnicodeError.encoding" title="Link to this definition">¶</a></dt>
  773. <dd><p>The name of the encoding that raised the error.</p>
  774. </dd></dl>
  775. <dl class="py attribute">
  776. <dt class="sig sig-object py" id="UnicodeError.reason">
  777. <span class="sig-name descname"><span class="pre">reason</span></span><a class="headerlink" href="#UnicodeError.reason" title="Link to this definition">¶</a></dt>
  778. <dd><p>A string describing the specific codec error.</p>
  779. </dd></dl>
  780. <dl class="py attribute">
  781. <dt class="sig sig-object py" id="UnicodeError.object">
  782. <span class="sig-name descname"><span class="pre">object</span></span><a class="headerlink" href="#UnicodeError.object" title="Link to this definition">¶</a></dt>
  783. <dd><p>The object the codec was attempting to encode or decode.</p>
  784. </dd></dl>
  785. <dl class="py attribute">
  786. <dt class="sig sig-object py" id="UnicodeError.start">
  787. <span class="sig-name descname"><span class="pre">start</span></span><a class="headerlink" href="#UnicodeError.start" title="Link to this definition">¶</a></dt>
  788. <dd><p>The first index of invalid data in <a class="reference internal" href="functions.html#object" title="object"><code class="xref py py-attr docutils literal notranslate"><span class="pre">object</span></code></a>.</p>
  789. </dd></dl>
  790. <dl class="py attribute">
  791. <dt class="sig sig-object py" id="UnicodeError.end">
  792. <span class="sig-name descname"><span class="pre">end</span></span><a class="headerlink" href="#UnicodeError.end" title="Link to this definition">¶</a></dt>
  793. <dd><p>The index after the last invalid data in <a class="reference internal" href="functions.html#object" title="object"><code class="xref py py-attr docutils literal notranslate"><span class="pre">object</span></code></a>.</p>
  794. </dd></dl>
  795. </dd></dl>
  796. <dl class="py exception">
  797. <dt class="sig sig-object py" id="UnicodeEncodeError">
  798. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">UnicodeEncodeError</span></span><a class="headerlink" href="#UnicodeEncodeError" title="Link to this definition">¶</a></dt>
  799. <dd><p>Raised when a Unicode-related error occurs during encoding. It is a subclass of
  800. <a class="reference internal" href="#UnicodeError" title="UnicodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeError</span></code></a>.</p>
  801. </dd></dl>
  802. <dl class="py exception">
  803. <dt class="sig sig-object py" id="UnicodeDecodeError">
  804. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">UnicodeDecodeError</span></span><a class="headerlink" href="#UnicodeDecodeError" title="Link to this definition">¶</a></dt>
  805. <dd><p>Raised when a Unicode-related error occurs during decoding. It is a subclass of
  806. <a class="reference internal" href="#UnicodeError" title="UnicodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeError</span></code></a>.</p>
  807. </dd></dl>
  808. <dl class="py exception">
  809. <dt class="sig sig-object py" id="UnicodeTranslateError">
  810. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">UnicodeTranslateError</span></span><a class="headerlink" href="#UnicodeTranslateError" title="Link to this definition">¶</a></dt>
  811. <dd><p>Raised when a Unicode-related error occurs during translating. It is a subclass
  812. of <a class="reference internal" href="#UnicodeError" title="UnicodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeError</span></code></a>.</p>
  813. </dd></dl>
  814. <dl class="py exception">
  815. <dt class="sig sig-object py" id="ValueError">
  816. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">ValueError</span></span><a class="headerlink" href="#ValueError" title="Link to this definition">¶</a></dt>
  817. <dd><p>Raised when an operation or function receives an argument that has the
  818. right type but an inappropriate value, and the situation is not described by a
  819. more precise exception such as <a class="reference internal" href="#IndexError" title="IndexError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndexError</span></code></a>.</p>
  820. </dd></dl>
  821. <dl class="py exception">
  822. <dt class="sig sig-object py" id="ZeroDivisionError">
  823. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">ZeroDivisionError</span></span><a class="headerlink" href="#ZeroDivisionError" title="Link to this definition">¶</a></dt>
  824. <dd><p>Raised when the second argument of a division or modulo operation is zero. The
  825. associated value is a string indicating the type of the operands and the
  826. operation.</p>
  827. </dd></dl>
  828. <p>The following exceptions are kept for compatibility with previous versions;
  829. starting from Python 3.3, they are aliases of <a class="reference internal" href="#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>.</p>
  830. <dl class="py exception">
  831. <dt class="sig sig-object py" id="EnvironmentError">
  832. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">EnvironmentError</span></span><a class="headerlink" href="#EnvironmentError" title="Link to this definition">¶</a></dt>
  833. <dd></dd></dl>
  834. <dl class="py exception">
  835. <dt class="sig sig-object py" id="IOError">
  836. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">IOError</span></span><a class="headerlink" href="#IOError" title="Link to this definition">¶</a></dt>
  837. <dd></dd></dl>
  838. <dl class="py exception">
  839. <dt class="sig sig-object py" id="WindowsError">
  840. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">WindowsError</span></span><a class="headerlink" href="#WindowsError" title="Link to this definition">¶</a></dt>
  841. <dd><p>Only available on Windows.</p>
  842. </dd></dl>
  843. <section id="os-exceptions">
  844. <h3>OS exceptions<a class="headerlink" href="#os-exceptions" title="Link to this heading">¶</a></h3>
  845. <p>The following exceptions are subclasses of <a class="reference internal" href="#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>, they get raised
  846. depending on the system error code.</p>
  847. <dl class="py exception">
  848. <dt class="sig sig-object py" id="BlockingIOError">
  849. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">BlockingIOError</span></span><a class="headerlink" href="#BlockingIOError" title="Link to this definition">¶</a></dt>
  850. <dd><p>Raised when an operation would block on an object (e.g. socket) set
  851. for non-blocking operation.
  852. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <a class="reference internal" href="errno.html#errno.EAGAIN" title="errno.EAGAIN"><code class="xref py py-const docutils literal notranslate"><span class="pre">EAGAIN</span></code></a>, <a class="reference internal" href="errno.html#errno.EALREADY" title="errno.EALREADY"><code class="xref py py-const docutils literal notranslate"><span class="pre">EALREADY</span></code></a>,
  853. <a class="reference internal" href="errno.html#errno.EWOULDBLOCK" title="errno.EWOULDBLOCK"><code class="xref py py-const docutils literal notranslate"><span class="pre">EWOULDBLOCK</span></code></a> and <a class="reference internal" href="errno.html#errno.EINPROGRESS" title="errno.EINPROGRESS"><code class="xref py py-const docutils literal notranslate"><span class="pre">EINPROGRESS</span></code></a>.</p>
  854. <p>In addition to those of <a class="reference internal" href="#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>, <a class="reference internal" href="#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BlockingIOError</span></code></a> can have
  855. one more attribute:</p>
  856. <dl class="py attribute">
  857. <dt class="sig sig-object py" id="BlockingIOError.characters_written">
  858. <span class="sig-name descname"><span class="pre">characters_written</span></span><a class="headerlink" href="#BlockingIOError.characters_written" title="Link to this definition">¶</a></dt>
  859. <dd><p>An integer containing the number of characters written to the stream
  860. before it blocked. This attribute is available when using the
  861. buffered I/O classes from the <a class="reference internal" href="io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a> module.</p>
  862. </dd></dl>
  863. </dd></dl>
  864. <dl class="py exception">
  865. <dt class="sig sig-object py" id="ChildProcessError">
  866. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">ChildProcessError</span></span><a class="headerlink" href="#ChildProcessError" title="Link to this definition">¶</a></dt>
  867. <dd><p>Raised when an operation on a child process failed.
  868. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <a class="reference internal" href="errno.html#errno.ECHILD" title="errno.ECHILD"><code class="xref py py-const docutils literal notranslate"><span class="pre">ECHILD</span></code></a>.</p>
  869. </dd></dl>
  870. <dl class="py exception">
  871. <dt class="sig sig-object py" id="ConnectionError">
  872. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">ConnectionError</span></span><a class="headerlink" href="#ConnectionError" title="Link to this definition">¶</a></dt>
  873. <dd><p>A base class for connection-related issues.</p>
  874. <p>Subclasses are <a class="reference internal" href="#BrokenPipeError" title="BrokenPipeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BrokenPipeError</span></code></a>, <a class="reference internal" href="#ConnectionAbortedError" title="ConnectionAbortedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionAbortedError</span></code></a>,
  875. <a class="reference internal" href="#ConnectionRefusedError" title="ConnectionRefusedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionRefusedError</span></code></a> and <a class="reference internal" href="#ConnectionResetError" title="ConnectionResetError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionResetError</span></code></a>.</p>
  876. </dd></dl>
  877. <dl class="py exception">
  878. <dt class="sig sig-object py" id="BrokenPipeError">
  879. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">BrokenPipeError</span></span><a class="headerlink" href="#BrokenPipeError" title="Link to this definition">¶</a></dt>
  880. <dd><p>A subclass of <a class="reference internal" href="#ConnectionError" title="ConnectionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionError</span></code></a>, raised when trying to write on a
  881. pipe while the other end has been closed, or trying to write on a socket
  882. which has been shutdown for writing.
  883. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <a class="reference internal" href="errno.html#errno.EPIPE" title="errno.EPIPE"><code class="xref py py-const docutils literal notranslate"><span class="pre">EPIPE</span></code></a> and <a class="reference internal" href="errno.html#errno.ESHUTDOWN" title="errno.ESHUTDOWN"><code class="xref py py-const docutils literal notranslate"><span class="pre">ESHUTDOWN</span></code></a>.</p>
  884. </dd></dl>
  885. <dl class="py exception">
  886. <dt class="sig sig-object py" id="ConnectionAbortedError">
  887. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">ConnectionAbortedError</span></span><a class="headerlink" href="#ConnectionAbortedError" title="Link to this definition">¶</a></dt>
  888. <dd><p>A subclass of <a class="reference internal" href="#ConnectionError" title="ConnectionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionError</span></code></a>, raised when a connection attempt
  889. is aborted by the peer.
  890. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <a class="reference internal" href="errno.html#errno.ECONNABORTED" title="errno.ECONNABORTED"><code class="xref py py-const docutils literal notranslate"><span class="pre">ECONNABORTED</span></code></a>.</p>
  891. </dd></dl>
  892. <dl class="py exception">
  893. <dt class="sig sig-object py" id="ConnectionRefusedError">
  894. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">ConnectionRefusedError</span></span><a class="headerlink" href="#ConnectionRefusedError" title="Link to this definition">¶</a></dt>
  895. <dd><p>A subclass of <a class="reference internal" href="#ConnectionError" title="ConnectionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionError</span></code></a>, raised when a connection attempt
  896. is refused by the peer.
  897. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <a class="reference internal" href="errno.html#errno.ECONNREFUSED" title="errno.ECONNREFUSED"><code class="xref py py-const docutils literal notranslate"><span class="pre">ECONNREFUSED</span></code></a>.</p>
  898. </dd></dl>
  899. <dl class="py exception">
  900. <dt class="sig sig-object py" id="ConnectionResetError">
  901. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">ConnectionResetError</span></span><a class="headerlink" href="#ConnectionResetError" title="Link to this definition">¶</a></dt>
  902. <dd><p>A subclass of <a class="reference internal" href="#ConnectionError" title="ConnectionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionError</span></code></a>, raised when a connection is
  903. reset by the peer.
  904. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <a class="reference internal" href="errno.html#errno.ECONNRESET" title="errno.ECONNRESET"><code class="xref py py-const docutils literal notranslate"><span class="pre">ECONNRESET</span></code></a>.</p>
  905. </dd></dl>
  906. <dl class="py exception">
  907. <dt class="sig sig-object py" id="FileExistsError">
  908. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">FileExistsError</span></span><a class="headerlink" href="#FileExistsError" title="Link to this definition">¶</a></dt>
  909. <dd><p>Raised when trying to create a file or directory which already exists.
  910. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <a class="reference internal" href="errno.html#errno.EEXIST" title="errno.EEXIST"><code class="xref py py-const docutils literal notranslate"><span class="pre">EEXIST</span></code></a>.</p>
  911. </dd></dl>
  912. <dl class="py exception">
  913. <dt class="sig sig-object py" id="FileNotFoundError">
  914. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">FileNotFoundError</span></span><a class="headerlink" href="#FileNotFoundError" title="Link to this definition">¶</a></dt>
  915. <dd><p>Raised when a file or directory is requested but doesn’t exist.
  916. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <a class="reference internal" href="errno.html#errno.ENOENT" title="errno.ENOENT"><code class="xref py py-const docutils literal notranslate"><span class="pre">ENOENT</span></code></a>.</p>
  917. </dd></dl>
  918. <dl class="py exception">
  919. <dt class="sig sig-object py" id="InterruptedError">
  920. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">InterruptedError</span></span><a class="headerlink" href="#InterruptedError" title="Link to this definition">¶</a></dt>
  921. <dd><p>Raised when a system call is interrupted by an incoming signal.
  922. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <a class="reference internal" href="errno.html#errno.EINTR" title="errno.EINTR"><code class="xref py py-const docutils literal notranslate"><span class="pre">EINTR</span></code></a>.</p>
  923. <div class="versionchanged">
  924. <p><span class="versionmodified changed">Changed in version 3.5: </span>Python now retries system calls when a syscall is interrupted by a
  925. signal, except if the signal handler raises an exception (see <span class="target" id="index-8"></span><a class="pep reference external" href="https://peps.python.org/pep-0475/"><strong>PEP 475</strong></a>
  926. for the rationale), instead of raising <a class="reference internal" href="#InterruptedError" title="InterruptedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InterruptedError</span></code></a>.</p>
  927. </div>
  928. </dd></dl>
  929. <dl class="py exception">
  930. <dt class="sig sig-object py" id="IsADirectoryError">
  931. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">IsADirectoryError</span></span><a class="headerlink" href="#IsADirectoryError" title="Link to this definition">¶</a></dt>
  932. <dd><p>Raised when a file operation (such as <a class="reference internal" href="os.html#os.remove" title="os.remove"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.remove()</span></code></a>) is requested
  933. on a directory.
  934. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <a class="reference internal" href="errno.html#errno.EISDIR" title="errno.EISDIR"><code class="xref py py-const docutils literal notranslate"><span class="pre">EISDIR</span></code></a>.</p>
  935. </dd></dl>
  936. <dl class="py exception">
  937. <dt class="sig sig-object py" id="NotADirectoryError">
  938. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">NotADirectoryError</span></span><a class="headerlink" href="#NotADirectoryError" title="Link to this definition">¶</a></dt>
  939. <dd><p>Raised when a directory operation (such as <a class="reference internal" href="os.html#os.listdir" title="os.listdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.listdir()</span></code></a>) is requested on
  940. something which is not a directory. On most POSIX platforms, it may also be
  941. raised if an operation attempts to open or traverse a non-directory file as if
  942. it were a directory.
  943. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <a class="reference internal" href="errno.html#errno.ENOTDIR" title="errno.ENOTDIR"><code class="xref py py-const docutils literal notranslate"><span class="pre">ENOTDIR</span></code></a>.</p>
  944. </dd></dl>
  945. <dl class="py exception">
  946. <dt class="sig sig-object py" id="PermissionError">
  947. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">PermissionError</span></span><a class="headerlink" href="#PermissionError" title="Link to this definition">¶</a></dt>
  948. <dd><p>Raised when trying to run an operation without the adequate access
  949. rights - for example filesystem permissions.
  950. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <a class="reference internal" href="errno.html#errno.EACCES" title="errno.EACCES"><code class="xref py py-const docutils literal notranslate"><span class="pre">EACCES</span></code></a>,
  951. <a class="reference internal" href="errno.html#errno.EPERM" title="errno.EPERM"><code class="xref py py-const docutils literal notranslate"><span class="pre">EPERM</span></code></a>, and <a class="reference internal" href="errno.html#errno.ENOTCAPABLE" title="errno.ENOTCAPABLE"><code class="xref py py-const docutils literal notranslate"><span class="pre">ENOTCAPABLE</span></code></a>.</p>
  952. <div class="versionchanged">
  953. <p><span class="versionmodified changed">Changed in version 3.11.1: </span>WASI’s <a class="reference internal" href="errno.html#errno.ENOTCAPABLE" title="errno.ENOTCAPABLE"><code class="xref py py-const docutils literal notranslate"><span class="pre">ENOTCAPABLE</span></code></a> is now mapped to
  954. <a class="reference internal" href="#PermissionError" title="PermissionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PermissionError</span></code></a>.</p>
  955. </div>
  956. </dd></dl>
  957. <dl class="py exception">
  958. <dt class="sig sig-object py" id="ProcessLookupError">
  959. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">ProcessLookupError</span></span><a class="headerlink" href="#ProcessLookupError" title="Link to this definition">¶</a></dt>
  960. <dd><p>Raised when a given process doesn’t exist.
  961. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <a class="reference internal" href="errno.html#errno.ESRCH" title="errno.ESRCH"><code class="xref py py-const docutils literal notranslate"><span class="pre">ESRCH</span></code></a>.</p>
  962. </dd></dl>
  963. <dl class="py exception">
  964. <dt class="sig sig-object py" id="TimeoutError">
  965. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">TimeoutError</span></span><a class="headerlink" href="#TimeoutError" title="Link to this definition">¶</a></dt>
  966. <dd><p>Raised when a system function timed out at the system level.
  967. Corresponds to <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> <a class="reference internal" href="errno.html#errno.ETIMEDOUT" title="errno.ETIMEDOUT"><code class="xref py py-const docutils literal notranslate"><span class="pre">ETIMEDOUT</span></code></a>.</p>
  968. </dd></dl>
  969. <div class="versionadded">
  970. <p><span class="versionmodified added">New in version 3.3: </span>All the above <a class="reference internal" href="#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> subclasses were added.</p>
  971. </div>
  972. <div class="admonition seealso">
  973. <p class="admonition-title">See also</p>
  974. <p><span class="target" id="index-9"></span><a class="pep reference external" href="https://peps.python.org/pep-3151/"><strong>PEP 3151</strong></a> - Reworking the OS and IO exception hierarchy</p>
  975. </div>
  976. </section>
  977. </section>
  978. <section id="warnings">
  979. <span id="warning-categories-as-exceptions"></span><h2>Warnings<a class="headerlink" href="#warnings" title="Link to this heading">¶</a></h2>
  980. <p>The following exceptions are used as warning categories; see the
  981. <a class="reference internal" href="warnings.html#warning-categories"><span class="std std-ref">Warning Categories</span></a> documentation for more details.</p>
  982. <dl class="py exception">
  983. <dt class="sig sig-object py" id="Warning">
  984. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">Warning</span></span><a class="headerlink" href="#Warning" title="Link to this definition">¶</a></dt>
  985. <dd><p>Base class for warning categories.</p>
  986. </dd></dl>
  987. <dl class="py exception">
  988. <dt class="sig sig-object py" id="UserWarning">
  989. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">UserWarning</span></span><a class="headerlink" href="#UserWarning" title="Link to this definition">¶</a></dt>
  990. <dd><p>Base class for warnings generated by user code.</p>
  991. </dd></dl>
  992. <dl class="py exception">
  993. <dt class="sig sig-object py" id="DeprecationWarning">
  994. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">DeprecationWarning</span></span><a class="headerlink" href="#DeprecationWarning" title="Link to this definition">¶</a></dt>
  995. <dd><p>Base class for warnings about deprecated features when those warnings are
  996. intended for other Python developers.</p>
  997. <p>Ignored by the default warning filters, except in the <code class="docutils literal notranslate"><span class="pre">__main__</span></code> module
  998. (<span class="target" id="index-10"></span><a class="pep reference external" href="https://peps.python.org/pep-0565/"><strong>PEP 565</strong></a>). Enabling the <a class="reference internal" href="devmode.html#devmode"><span class="std std-ref">Python Development Mode</span></a> shows
  999. this warning.</p>
  1000. <p>The deprecation policy is described in <span class="target" id="index-11"></span><a class="pep reference external" href="https://peps.python.org/pep-0387/"><strong>PEP 387</strong></a>.</p>
  1001. </dd></dl>
  1002. <dl class="py exception">
  1003. <dt class="sig sig-object py" id="PendingDeprecationWarning">
  1004. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">PendingDeprecationWarning</span></span><a class="headerlink" href="#PendingDeprecationWarning" title="Link to this definition">¶</a></dt>
  1005. <dd><p>Base class for warnings about features which are obsolete and
  1006. expected to be deprecated in the future, but are not deprecated
  1007. at the moment.</p>
  1008. <p>This class is rarely used as emitting a warning about a possible
  1009. upcoming deprecation is unusual, and <a class="reference internal" href="#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a>
  1010. is preferred for already active deprecations.</p>
  1011. <p>Ignored by the default warning filters. Enabling the <a class="reference internal" href="devmode.html#devmode"><span class="std std-ref">Python
  1012. Development Mode</span></a> shows this warning.</p>
  1013. <p>The deprecation policy is described in <span class="target" id="index-12"></span><a class="pep reference external" href="https://peps.python.org/pep-0387/"><strong>PEP 387</strong></a>.</p>
  1014. </dd></dl>
  1015. <dl class="py exception">
  1016. <dt class="sig sig-object py" id="SyntaxWarning">
  1017. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">SyntaxWarning</span></span><a class="headerlink" href="#SyntaxWarning" title="Link to this definition">¶</a></dt>
  1018. <dd><p>Base class for warnings about dubious syntax.</p>
  1019. </dd></dl>
  1020. <dl class="py exception">
  1021. <dt class="sig sig-object py" id="RuntimeWarning">
  1022. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">RuntimeWarning</span></span><a class="headerlink" href="#RuntimeWarning" title="Link to this definition">¶</a></dt>
  1023. <dd><p>Base class for warnings about dubious runtime behavior.</p>
  1024. </dd></dl>
  1025. <dl class="py exception">
  1026. <dt class="sig sig-object py" id="FutureWarning">
  1027. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">FutureWarning</span></span><a class="headerlink" href="#FutureWarning" title="Link to this definition">¶</a></dt>
  1028. <dd><p>Base class for warnings about deprecated features when those warnings are
  1029. intended for end users of applications that are written in Python.</p>
  1030. </dd></dl>
  1031. <dl class="py exception">
  1032. <dt class="sig sig-object py" id="ImportWarning">
  1033. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">ImportWarning</span></span><a class="headerlink" href="#ImportWarning" title="Link to this definition">¶</a></dt>
  1034. <dd><p>Base class for warnings about probable mistakes in module imports.</p>
  1035. <p>Ignored by the default warning filters. Enabling the <a class="reference internal" href="devmode.html#devmode"><span class="std std-ref">Python
  1036. Development Mode</span></a> shows this warning.</p>
  1037. </dd></dl>
  1038. <dl class="py exception">
  1039. <dt class="sig sig-object py" id="UnicodeWarning">
  1040. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">UnicodeWarning</span></span><a class="headerlink" href="#UnicodeWarning" title="Link to this definition">¶</a></dt>
  1041. <dd><p>Base class for warnings related to Unicode.</p>
  1042. </dd></dl>
  1043. <dl class="py exception">
  1044. <dt class="sig sig-object py" id="EncodingWarning">
  1045. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">EncodingWarning</span></span><a class="headerlink" href="#EncodingWarning" title="Link to this definition">¶</a></dt>
  1046. <dd><p>Base class for warnings related to encodings.</p>
  1047. <p>See <a class="reference internal" href="io.html#io-encoding-warning"><span class="std std-ref">Opt-in EncodingWarning</span></a> for details.</p>
  1048. <div class="versionadded">
  1049. <p><span class="versionmodified added">New in version 3.10.</span></p>
  1050. </div>
  1051. </dd></dl>
  1052. <dl class="py exception">
  1053. <dt class="sig sig-object py" id="BytesWarning">
  1054. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">BytesWarning</span></span><a class="headerlink" href="#BytesWarning" title="Link to this definition">¶</a></dt>
  1055. <dd><p>Base class for warnings related to <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> and <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a>.</p>
  1056. </dd></dl>
  1057. <dl class="py exception">
  1058. <dt class="sig sig-object py" id="ResourceWarning">
  1059. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">ResourceWarning</span></span><a class="headerlink" href="#ResourceWarning" title="Link to this definition">¶</a></dt>
  1060. <dd><p>Base class for warnings related to resource usage.</p>
  1061. <p>Ignored by the default warning filters. Enabling the <a class="reference internal" href="devmode.html#devmode"><span class="std std-ref">Python
  1062. Development Mode</span></a> shows this warning.</p>
  1063. <div class="versionadded">
  1064. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1065. </div>
  1066. </dd></dl>
  1067. </section>
  1068. <section id="exception-groups">
  1069. <span id="lib-exception-groups"></span><h2>Exception groups<a class="headerlink" href="#exception-groups" title="Link to this heading">¶</a></h2>
  1070. <p>The following are used when it is necessary to raise multiple unrelated
  1071. exceptions. They are part of the exception hierarchy so they can be
  1072. handled with <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> like all other exceptions. In addition,
  1073. they are recognised by <a class="reference internal" href="../reference/compound_stmts.html#except-star"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except*</span></code></a>, which matches
  1074. their subgroups based on the types of the contained exceptions.</p>
  1075. <dl class="py exception">
  1076. <dt class="sig sig-object py" id="ExceptionGroup">
  1077. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">ExceptionGroup</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">excs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ExceptionGroup" title="Link to this definition">¶</a></dt>
  1078. <dd></dd></dl>
  1079. <dl class="py exception">
  1080. <dt class="sig sig-object py" id="BaseExceptionGroup">
  1081. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">BaseExceptionGroup</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">excs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#BaseExceptionGroup" title="Link to this definition">¶</a></dt>
  1082. <dd><p>Both of these exception types wrap the exceptions in the sequence <code class="docutils literal notranslate"><span class="pre">excs</span></code>.
  1083. The <code class="docutils literal notranslate"><span class="pre">msg</span></code> parameter must be a string. The difference between the two
  1084. classes is that <a class="reference internal" href="#BaseExceptionGroup" title="BaseExceptionGroup"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseExceptionGroup</span></code></a> extends <a class="reference internal" href="#BaseException" title="BaseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseException</span></code></a> and
  1085. it can wrap any exception, while <a class="reference internal" href="#ExceptionGroup" title="ExceptionGroup"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ExceptionGroup</span></code></a> extends <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a>
  1086. and it can only wrap subclasses of <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a>. This design is so that
  1087. <code class="docutils literal notranslate"><span class="pre">except</span> <span class="pre">Exception</span></code> catches an <a class="reference internal" href="#ExceptionGroup" title="ExceptionGroup"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ExceptionGroup</span></code></a> but not
  1088. <a class="reference internal" href="#BaseExceptionGroup" title="BaseExceptionGroup"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseExceptionGroup</span></code></a>.</p>
  1089. <p>The <a class="reference internal" href="#BaseExceptionGroup" title="BaseExceptionGroup"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseExceptionGroup</span></code></a> constructor returns an <a class="reference internal" href="#ExceptionGroup" title="ExceptionGroup"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ExceptionGroup</span></code></a>
  1090. rather than a <a class="reference internal" href="#BaseExceptionGroup" title="BaseExceptionGroup"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseExceptionGroup</span></code></a> if all contained exceptions are
  1091. <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a> instances, so it can be used to make the selection
  1092. automatic. The <a class="reference internal" href="#ExceptionGroup" title="ExceptionGroup"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ExceptionGroup</span></code></a> constructor, on the other hand,
  1093. raises a <a class="reference internal" href="#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if any contained exception is not an
  1094. <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a> subclass.</p>
  1095. <dl class="py attribute">
  1096. <dt class="sig sig-object py" id="BaseExceptionGroup.message">
  1097. <span class="sig-name descname"><span class="pre">message</span></span><a class="headerlink" href="#BaseExceptionGroup.message" title="Link to this definition">¶</a></dt>
  1098. <dd><p>The <code class="docutils literal notranslate"><span class="pre">msg</span></code> argument to the constructor. This is a read-only attribute.</p>
  1099. </dd></dl>
  1100. <dl class="py attribute">
  1101. <dt class="sig sig-object py" id="BaseExceptionGroup.exceptions">
  1102. <span class="sig-name descname"><span class="pre">exceptions</span></span><a class="headerlink" href="#BaseExceptionGroup.exceptions" title="Link to this definition">¶</a></dt>
  1103. <dd><p>A tuple of the exceptions in the <code class="docutils literal notranslate"><span class="pre">excs</span></code> sequence given to the
  1104. constructor. This is a read-only attribute.</p>
  1105. </dd></dl>
  1106. <dl class="py method">
  1107. <dt class="sig sig-object py" id="BaseExceptionGroup.subgroup">
  1108. <span class="sig-name descname"><span class="pre">subgroup</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">condition</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#BaseExceptionGroup.subgroup" title="Link to this definition">¶</a></dt>
  1109. <dd><p>Returns an exception group that contains only the exceptions from the
  1110. current group that match <em>condition</em>, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if the result is empty.</p>
  1111. <p>The condition can be either a function that accepts an exception and returns
  1112. true for those that should be in the subgroup, or it can be an exception type
  1113. or a tuple of exception types, which is used to check for a match using the
  1114. same check that is used in an <code class="docutils literal notranslate"><span class="pre">except</span></code> clause.</p>
  1115. <p>The nesting structure of the current exception is preserved in the result,
  1116. as are the values of its <a class="reference internal" href="#BaseExceptionGroup.message" title="BaseExceptionGroup.message"><code class="xref py py-attr docutils literal notranslate"><span class="pre">message</span></code></a>,
  1117. <a class="reference internal" href="#BaseException.__traceback__" title="BaseException.__traceback__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__traceback__</span></code></a>, <a class="reference internal" href="#BaseException.__cause__" title="BaseException.__cause__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code></a>,
  1118. <a class="reference internal" href="#BaseException.__context__" title="BaseException.__context__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__context__</span></code></a> and
  1119. <a class="reference internal" href="#BaseException.__notes__" title="BaseException.__notes__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__notes__</span></code></a> fields.
  1120. Empty nested groups are omitted from the result.</p>
  1121. <p>The condition is checked for all exceptions in the nested exception group,
  1122. including the top-level and any nested exception groups. If the condition is
  1123. true for such an exception group, it is included in the result in full.</p>
  1124. </dd></dl>
  1125. <dl class="py method">
  1126. <dt class="sig sig-object py" id="BaseExceptionGroup.split">
  1127. <span class="sig-name descname"><span class="pre">split</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">condition</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#BaseExceptionGroup.split" title="Link to this definition">¶</a></dt>
  1128. <dd><p>Like <a class="reference internal" href="#BaseExceptionGroup.subgroup" title="BaseExceptionGroup.subgroup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">subgroup()</span></code></a>, but returns the pair <code class="docutils literal notranslate"><span class="pre">(match,</span> <span class="pre">rest)</span></code> where <code class="docutils literal notranslate"><span class="pre">match</span></code>
  1129. is <code class="docutils literal notranslate"><span class="pre">subgroup(condition)</span></code> and <code class="docutils literal notranslate"><span class="pre">rest</span></code> is the remaining non-matching
  1130. part.</p>
  1131. </dd></dl>
  1132. <dl class="py method">
  1133. <dt class="sig sig-object py" id="BaseExceptionGroup.derive">
  1134. <span class="sig-name descname"><span class="pre">derive</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">excs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#BaseExceptionGroup.derive" title="Link to this definition">¶</a></dt>
  1135. <dd><p>Returns an exception group with the same <a class="reference internal" href="#BaseExceptionGroup.message" title="BaseExceptionGroup.message"><code class="xref py py-attr docutils literal notranslate"><span class="pre">message</span></code></a>, but which
  1136. wraps the exceptions in <code class="docutils literal notranslate"><span class="pre">excs</span></code>.</p>
  1137. <p>This method is used by <a class="reference internal" href="#BaseExceptionGroup.subgroup" title="BaseExceptionGroup.subgroup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">subgroup()</span></code></a> and <a class="reference internal" href="#BaseExceptionGroup.split" title="BaseExceptionGroup.split"><code class="xref py py-meth docutils literal notranslate"><span class="pre">split()</span></code></a>. A
  1138. subclass needs to override it in order to make <a class="reference internal" href="#BaseExceptionGroup.subgroup" title="BaseExceptionGroup.subgroup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">subgroup()</span></code></a>
  1139. and <a class="reference internal" href="#BaseExceptionGroup.split" title="BaseExceptionGroup.split"><code class="xref py py-meth docutils literal notranslate"><span class="pre">split()</span></code></a> return instances of the subclass rather
  1140. than <a class="reference internal" href="#ExceptionGroup" title="ExceptionGroup"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ExceptionGroup</span></code></a>.</p>
  1141. <p><a class="reference internal" href="#BaseExceptionGroup.subgroup" title="BaseExceptionGroup.subgroup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">subgroup()</span></code></a> and <a class="reference internal" href="#BaseExceptionGroup.split" title="BaseExceptionGroup.split"><code class="xref py py-meth docutils literal notranslate"><span class="pre">split()</span></code></a> copy the
  1142. <a class="reference internal" href="#BaseException.__traceback__" title="BaseException.__traceback__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__traceback__</span></code></a>,
  1143. <a class="reference internal" href="#BaseException.__cause__" title="BaseException.__cause__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code></a>, <a class="reference internal" href="#BaseException.__context__" title="BaseException.__context__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__context__</span></code></a> and
  1144. <a class="reference internal" href="#BaseException.__notes__" title="BaseException.__notes__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__notes__</span></code></a> fields from
  1145. the original exception group to the one returned by <a class="reference internal" href="#BaseExceptionGroup.derive" title="BaseExceptionGroup.derive"><code class="xref py py-meth docutils literal notranslate"><span class="pre">derive()</span></code></a>, so
  1146. these fields do not need to be updated by <a class="reference internal" href="#BaseExceptionGroup.derive" title="BaseExceptionGroup.derive"><code class="xref py py-meth docutils literal notranslate"><span class="pre">derive()</span></code></a>.</p>
  1147. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">MyGroup</span><span class="p">(</span><span class="n">ExceptionGroup</span><span class="p">):</span>
  1148. <span class="gp">... </span> <span class="k">def</span> <span class="nf">derive</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">excs</span><span class="p">):</span>
  1149. <span class="gp">... </span> <span class="k">return</span> <span class="n">MyGroup</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="n">excs</span><span class="p">)</span>
  1150. <span class="gp">...</span>
  1151. <span class="gp">&gt;&gt;&gt; </span><span class="n">e</span> <span class="o">=</span> <span class="n">MyGroup</span><span class="p">(</span><span class="s2">&quot;eg&quot;</span><span class="p">,</span> <span class="p">[</span><span class="ne">ValueError</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="ne">TypeError</span><span class="p">(</span><span class="mi">2</span><span class="p">)])</span>
  1152. <span class="gp">&gt;&gt;&gt; </span><span class="n">e</span><span class="o">.</span><span class="n">add_note</span><span class="p">(</span><span class="s2">&quot;a note&quot;</span><span class="p">)</span>
  1153. <span class="gp">&gt;&gt;&gt; </span><span class="n">e</span><span class="o">.</span><span class="n">__context__</span> <span class="o">=</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">&quot;context&quot;</span><span class="p">)</span>
  1154. <span class="gp">&gt;&gt;&gt; </span><span class="n">e</span><span class="o">.</span><span class="n">__cause__</span> <span class="o">=</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">&quot;cause&quot;</span><span class="p">)</span>
  1155. <span class="gp">&gt;&gt;&gt; </span><span class="k">try</span><span class="p">:</span>
  1156. <span class="gp">... </span> <span class="k">raise</span> <span class="n">e</span>
  1157. <span class="gp">... </span><span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
  1158. <span class="gp">... </span> <span class="n">exc</span> <span class="o">=</span> <span class="n">e</span>
  1159. <span class="gp">...</span>
  1160. <span class="gp">&gt;&gt;&gt; </span><span class="n">match</span><span class="p">,</span> <span class="n">rest</span> <span class="o">=</span> <span class="n">exc</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="ne">ValueError</span><span class="p">)</span>
  1161. <span class="gp">&gt;&gt;&gt; </span><span class="n">exc</span><span class="p">,</span> <span class="n">exc</span><span class="o">.</span><span class="n">__context__</span><span class="p">,</span> <span class="n">exc</span><span class="o">.</span><span class="n">__cause__</span><span class="p">,</span> <span class="n">exc</span><span class="o">.</span><span class="n">__notes__</span>
  1162. <span class="go">(MyGroup(&#39;eg&#39;, [ValueError(1), TypeError(2)]), Exception(&#39;context&#39;), Exception(&#39;cause&#39;), [&#39;a note&#39;])</span>
  1163. <span class="gp">&gt;&gt;&gt; </span><span class="n">match</span><span class="p">,</span> <span class="n">match</span><span class="o">.</span><span class="n">__context__</span><span class="p">,</span> <span class="n">match</span><span class="o">.</span><span class="n">__cause__</span><span class="p">,</span> <span class="n">match</span><span class="o">.</span><span class="n">__notes__</span>
  1164. <span class="go">(MyGroup(&#39;eg&#39;, [ValueError(1)]), Exception(&#39;context&#39;), Exception(&#39;cause&#39;), [&#39;a note&#39;])</span>
  1165. <span class="gp">&gt;&gt;&gt; </span><span class="n">rest</span><span class="p">,</span> <span class="n">rest</span><span class="o">.</span><span class="n">__context__</span><span class="p">,</span> <span class="n">rest</span><span class="o">.</span><span class="n">__cause__</span><span class="p">,</span> <span class="n">rest</span><span class="o">.</span><span class="n">__notes__</span>
  1166. <span class="go">(MyGroup(&#39;eg&#39;, [TypeError(2)]), Exception(&#39;context&#39;), Exception(&#39;cause&#39;), [&#39;a note&#39;])</span>
  1167. <span class="gp">&gt;&gt;&gt; </span><span class="n">exc</span><span class="o">.</span><span class="n">__traceback__</span> <span class="ow">is</span> <span class="n">match</span><span class="o">.</span><span class="n">__traceback__</span> <span class="ow">is</span> <span class="n">rest</span><span class="o">.</span><span class="n">__traceback__</span>
  1168. <span class="go">True</span>
  1169. </pre></div>
  1170. </div>
  1171. </dd></dl>
  1172. <p>Note that <a class="reference internal" href="#BaseExceptionGroup" title="BaseExceptionGroup"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseExceptionGroup</span></code></a> defines <a class="reference internal" href="../reference/datamodel.html#object.__new__" title="object.__new__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__new__()</span></code></a>, so
  1173. subclasses that need a different constructor signature need to
  1174. override that rather than <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code></a>. For example, the following
  1175. defines an exception group subclass which accepts an exit_code and
  1176. and constructs the group’s message from it.</p>
  1177. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Errors</span><span class="p">(</span><span class="n">ExceptionGroup</span><span class="p">):</span>
  1178. <span class="k">def</span> <span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">errors</span><span class="p">,</span> <span class="n">exit_code</span><span class="p">):</span>
  1179. <span class="bp">self</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="n">Errors</span><span class="p">,</span> <span class="sa">f</span><span class="s2">&quot;exit code: </span><span class="si">{</span><span class="n">exit_code</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">errors</span><span class="p">)</span>
  1180. <span class="bp">self</span><span class="o">.</span><span class="n">exit_code</span> <span class="o">=</span> <span class="n">exit_code</span>
  1181. <span class="k">return</span> <span class="bp">self</span>
  1182. <span class="k">def</span> <span class="nf">derive</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">excs</span><span class="p">):</span>
  1183. <span class="k">return</span> <span class="n">Errors</span><span class="p">(</span><span class="n">excs</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">exit_code</span><span class="p">)</span>
  1184. </pre></div>
  1185. </div>
  1186. <p>Like <a class="reference internal" href="#ExceptionGroup" title="ExceptionGroup"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ExceptionGroup</span></code></a>, any subclass of <a class="reference internal" href="#BaseExceptionGroup" title="BaseExceptionGroup"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseExceptionGroup</span></code></a> which
  1187. is also a subclass of <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a> can only wrap instances of
  1188. <a class="reference internal" href="#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a>.</p>
  1189. <div class="versionadded">
  1190. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1191. </div>
  1192. </dd></dl>
  1193. </section>
  1194. <section id="exception-hierarchy">
  1195. <h2>Exception hierarchy<a class="headerlink" href="#exception-hierarchy" title="Link to this heading">¶</a></h2>
  1196. <p>The class hierarchy for built-in exceptions is:</p>
  1197. <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>BaseException
  1198. ├── BaseExceptionGroup
  1199. ├── GeneratorExit
  1200. ├── KeyboardInterrupt
  1201. ├── SystemExit
  1202. └── Exception
  1203. ├── ArithmeticError
  1204. │ ├── FloatingPointError
  1205. │ ├── OverflowError
  1206. │ └── ZeroDivisionError
  1207. ├── AssertionError
  1208. ├── AttributeError
  1209. ├── BufferError
  1210. ├── EOFError
  1211. ├── ExceptionGroup [BaseExceptionGroup]
  1212. ├── ImportError
  1213. │ └── ModuleNotFoundError
  1214. ├── LookupError
  1215. │ ├── IndexError
  1216. │ └── KeyError
  1217. ├── MemoryError
  1218. ├── NameError
  1219. │ └── UnboundLocalError
  1220. ├── OSError
  1221. │ ├── BlockingIOError
  1222. │ ├── ChildProcessError
  1223. │ ├── ConnectionError
  1224. │ │ ├── BrokenPipeError
  1225. │ │ ├── ConnectionAbortedError
  1226. │ │ ├── ConnectionRefusedError
  1227. │ │ └── ConnectionResetError
  1228. │ ├── FileExistsError
  1229. │ ├── FileNotFoundError
  1230. │ ├── InterruptedError
  1231. │ ├── IsADirectoryError
  1232. │ ├── NotADirectoryError
  1233. │ ├── PermissionError
  1234. │ ├── ProcessLookupError
  1235. │ └── TimeoutError
  1236. ├── ReferenceError
  1237. ├── RuntimeError
  1238. │ ├── NotImplementedError
  1239. │ └── RecursionError
  1240. ├── StopAsyncIteration
  1241. ├── StopIteration
  1242. ├── SyntaxError
  1243. │ └── IndentationError
  1244. │ └── TabError
  1245. ├── SystemError
  1246. ├── TypeError
  1247. ├── ValueError
  1248. │ └── UnicodeError
  1249. │ ├── UnicodeDecodeError
  1250. │ ├── UnicodeEncodeError
  1251. │ └── UnicodeTranslateError
  1252. └── Warning
  1253. ├── BytesWarning
  1254. ├── DeprecationWarning
  1255. ├── EncodingWarning
  1256. ├── FutureWarning
  1257. ├── ImportWarning
  1258. ├── PendingDeprecationWarning
  1259. ├── ResourceWarning
  1260. ├── RuntimeWarning
  1261. ├── SyntaxWarning
  1262. ├── UnicodeWarning
  1263. └── UserWarning
  1264. </pre></div>
  1265. </div>
  1266. </section>
  1267. </section>
  1268. <div class="clearer"></div>
  1269. </div>
  1270. </div>
  1271. </div>
  1272. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1273. <div class="sphinxsidebarwrapper">
  1274. <div>
  1275. <h3><a href="../contents.html">Table of Contents</a></h3>
  1276. <ul>
  1277. <li><a class="reference internal" href="#">Built-in Exceptions</a><ul>
  1278. <li><a class="reference internal" href="#exception-context">Exception context</a></li>
  1279. <li><a class="reference internal" href="#inheriting-from-built-in-exceptions">Inheriting from built-in exceptions</a></li>
  1280. <li><a class="reference internal" href="#base-classes">Base classes</a></li>
  1281. <li><a class="reference internal" href="#concrete-exceptions">Concrete exceptions</a><ul>
  1282. <li><a class="reference internal" href="#os-exceptions">OS exceptions</a></li>
  1283. </ul>
  1284. </li>
  1285. <li><a class="reference internal" href="#warnings">Warnings</a></li>
  1286. <li><a class="reference internal" href="#exception-groups">Exception groups</a></li>
  1287. <li><a class="reference internal" href="#exception-hierarchy">Exception hierarchy</a></li>
  1288. </ul>
  1289. </li>
  1290. </ul>
  1291. </div>
  1292. <div>
  1293. <h4>Previous topic</h4>
  1294. <p class="topless"><a href="stdtypes.html"
  1295. title="previous chapter">Built-in Types</a></p>
  1296. </div>
  1297. <div>
  1298. <h4>Next topic</h4>
  1299. <p class="topless"><a href="text.html"
  1300. title="next chapter">Text Processing Services</a></p>
  1301. </div>
  1302. <div role="note" aria-label="source link">
  1303. <h3>This Page</h3>
  1304. <ul class="this-page-menu">
  1305. <li><a href="../bugs.html">Report a Bug</a></li>
  1306. <li>
  1307. <a href="https://github.com/python/cpython/blob/main/Doc/library/exceptions.rst"
  1308. rel="nofollow">Show Source
  1309. </a>
  1310. </li>
  1311. </ul>
  1312. </div>
  1313. </div>
  1314. <div id="sidebarbutton" title="Collapse sidebar">
  1315. <span>«</span>
  1316. </div>
  1317. </div>
  1318. <div class="clearer"></div>
  1319. </div>
  1320. <div class="related" role="navigation" aria-label="related navigation">
  1321. <h3>Navigation</h3>
  1322. <ul>
  1323. <li class="right" style="margin-right: 10px">
  1324. <a href="../genindex.html" title="General Index"
  1325. >index</a></li>
  1326. <li class="right" >
  1327. <a href="../py-modindex.html" title="Python Module Index"
  1328. >modules</a> |</li>
  1329. <li class="right" >
  1330. <a href="text.html" title="Text Processing Services"
  1331. >next</a> |</li>
  1332. <li class="right" >
  1333. <a href="stdtypes.html" title="Built-in Types"
  1334. >previous</a> |</li>
  1335. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1336. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1337. <li class="switchers">
  1338. <div class="language_switcher_placeholder"></div>
  1339. <div class="version_switcher_placeholder"></div>
  1340. </li>
  1341. <li>
  1342. </li>
  1343. <li id="cpython-language-and-version">
  1344. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1345. </li>
  1346. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1347. <li class="nav-item nav-item-this"><a href="">Built-in Exceptions</a></li>
  1348. <li class="right">
  1349. <div class="inline-search" role="search">
  1350. <form class="inline-search" action="../search.html" method="get">
  1351. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1352. <input type="submit" value="Go" />
  1353. </form>
  1354. </div>
  1355. |
  1356. </li>
  1357. <li class="right">
  1358. <label class="theme-selector-label">
  1359. Theme
  1360. <select class="theme-selector" oninput="activateTheme(this.value)">
  1361. <option value="auto" selected>Auto</option>
  1362. <option value="light">Light</option>
  1363. <option value="dark">Dark</option>
  1364. </select>
  1365. </label> |</li>
  1366. </ul>
  1367. </div>
  1368. <div class="footer">
  1369. &copy;
  1370. <a href="../copyright.html">
  1371. Copyright
  1372. </a>
  1373. 2001-2024, Python Software Foundation.
  1374. <br />
  1375. This page is licensed under the Python Software Foundation License Version 2.
  1376. <br />
  1377. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1378. <br />
  1379. See <a href="/license.html">History and License</a> for more information.<br />
  1380. <br />
  1381. The Python Software Foundation is a non-profit corporation.
  1382. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1383. <br />
  1384. <br />
  1385. Last updated on Apr 09, 2024 (13:47 UTC).
  1386. <a href="/bugs.html">Found a bug</a>?
  1387. <br />
  1388. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1389. </div>
  1390. </body>
  1391. </html>
上海开阖软件有限公司 沪ICP备12045867号-1