| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | .. _bltin-exceptions: |
| 2 | |
| 3 | Built-in Exceptions |
| 4 | =================== |
| 5 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 6 | .. index:: |
| 7 | statement: try |
| 8 | statement: except |
| 9 | |
| Georg Brandl | fbd1b22 | 2009-12-29 21:38:35 +0000 | [diff] [blame] | 10 | In Python, all exceptions must be instances of a class that derives from |
| 11 | :class:`BaseException`. In a :keyword:`try` statement with an :keyword:`except` |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | clause that mentions a particular class, that clause also handles any exception |
| 13 | classes derived from that class (but not exception classes from which *it* is |
| 14 | derived). Two exception classes that are not related via subclassing are never |
| 15 | equivalent, even if they have the same name. |
| 16 | |
| 17 | .. index:: statement: raise |
| 18 | |
| 19 | The built-in exceptions listed below can be generated by the interpreter or |
| 20 | built-in functions. Except where mentioned, they have an "associated value" |
| Georg Brandl | fb6fd5d | 2011-01-07 18:28:45 +0000 | [diff] [blame] | 21 | indicating the detailed cause of the error. This may be a string or a tuple of |
| 22 | several items of information (e.g., an error code and a string explaining the |
| 23 | code). The associated value is usually passed as arguments to the exception |
| 24 | class's constructor. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | |
| 26 | User code can raise built-in exceptions. This can be used to test an exception |
| 27 | handler or to report an error condition "just like" the situation in which the |
| 28 | interpreter raises the same exception; but beware that there is nothing to |
| 29 | prevent user code from raising an inappropriate error. |
| 30 | |
| Mark Dickinson | abf079d | 2014-04-14 11:20:12 -0400 | [diff] [blame] | 31 | The built-in exception classes can be subclassed to define new exceptions; |
| 32 | programmers are encouraged to derive new exceptions from the :exc:`Exception` |
| 33 | class or one of its subclasses, and not from :exc:`BaseException`. More |
| 34 | information on defining exceptions is available in the Python Tutorial under |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 35 | :ref:`tut-userexceptions`. |
| 36 | |
| Miss Islington (bot) | b2a9899 | 2021-10-20 11:50:09 -0700 | [diff] [blame^] | 37 | |
| 38 | Exception context |
| 39 | ----------------- |
| 40 | |
| Georg Brandl | e4196d3 | 2014-10-31 09:41:46 +0100 | [diff] [blame] | 41 | When raising (or re-raising) an exception in an :keyword:`except` or |
| 42 | :keyword:`finally` clause |
| Nick Coghlan | ab7bf21 | 2012-02-26 17:49:52 +1000 | [diff] [blame] | 43 | :attr:`__context__` is automatically set to the last exception caught; if the |
| 44 | new exception is not handled the traceback that is eventually displayed will |
| 45 | include the originating exception(s) and the final exception. |
| 46 | |
| Nick Coghlan | 0eee97c | 2012-12-09 16:21:46 +1000 | [diff] [blame] | 47 | When raising a new exception (rather than using a bare ``raise`` to re-raise |
| 48 | the exception currently being handled), the implicit exception context can be |
| sblondon | 2fd928c | 2021-04-29 20:02:40 +0200 | [diff] [blame] | 49 | supplemented with an explicit cause by using :keyword:`from<raise>` with |
| Nick Coghlan | 0eee97c | 2012-12-09 16:21:46 +1000 | [diff] [blame] | 50 | :keyword:`raise`:: |
| 51 | |
| 52 | raise new_exc from original_exc |
| 53 | |
| sblondon | 2fd928c | 2021-04-29 20:02:40 +0200 | [diff] [blame] | 54 | The expression following :keyword:`from<raise>` must be an exception or ``None``. It |
| Nick Coghlan | 0eee97c | 2012-12-09 16:21:46 +1000 | [diff] [blame] | 55 | will be set as :attr:`__cause__` on the raised exception. Setting |
| 56 | :attr:`__cause__` also implicitly sets the :attr:`__suppress_context__` |
| 57 | attribute to ``True``, so that using ``raise new_exc from None`` |
| 58 | effectively replaces the old exception with the new one for display |
| Andre Delfino | 55f41e4 | 2018-12-05 16:45:30 -0300 | [diff] [blame] | 59 | purposes (e.g. converting :exc:`KeyError` to :exc:`AttributeError`), while |
| Nick Coghlan | 0eee97c | 2012-12-09 16:21:46 +1000 | [diff] [blame] | 60 | leaving the old exception available in :attr:`__context__` for introspection |
| 61 | when debugging. |
| Nick Coghlan | ab7bf21 | 2012-02-26 17:49:52 +1000 | [diff] [blame] | 62 | |
| Nick Coghlan | 8e18fc8 | 2012-12-08 21:39:24 +1000 | [diff] [blame] | 63 | The default traceback display code shows these chained exceptions in |
| 64 | addition to the traceback for the exception itself. An explicitly chained |
| 65 | exception in :attr:`__cause__` is always shown when present. An implicitly |
| 66 | chained exception in :attr:`__context__` is shown only if :attr:`__cause__` |
| Nick Coghlan | 0eee97c | 2012-12-09 16:21:46 +1000 | [diff] [blame] | 67 | is :const:`None` and :attr:`__suppress_context__` is false. |
| Nick Coghlan | 8e18fc8 | 2012-12-08 21:39:24 +1000 | [diff] [blame] | 68 | |
| 69 | In either case, the exception itself is always shown after any chained |
| 70 | exceptions so that the final line of the traceback always shows the last |
| 71 | exception that was raised. |
| Nick Coghlan | ab7bf21 | 2012-02-26 17:49:52 +1000 | [diff] [blame] | 72 | |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 73 | |
| Miss Islington (bot) | b2a9899 | 2021-10-20 11:50:09 -0700 | [diff] [blame^] | 74 | Inheriting from built-in exceptions |
| 75 | ----------------------------------- |
| 76 | |
| 77 | User code can create subclasses that inherit from an exception type. |
| 78 | It's recommended to only subclass one exception type at a time to avoid |
| 79 | any possible conflicts between how the bases handle the ``args`` |
| 80 | attribute, as well as due to possible memory layout incompatibilities. |
| 81 | |
| 82 | .. impl-detail:: |
| 83 | |
| 84 | Most built-in exceptions are implemented in C for efficiency, see: |
| 85 | :source:`Objects/exceptions.c`. Some have custom memory layouts |
| 86 | which makes it impossible to create a subclass that inherits from |
| 87 | multiple exception types. The memory layout of a type is an implementation |
| 88 | detail and might change between Python versions, leading to new |
| 89 | conflicts in the future. Therefore, it's recommended to avoid |
| 90 | subclassing multiple exception types altogether. |
| 91 | |
| 92 | |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 93 | Base classes |
| 94 | ------------ |
| 95 | |
| Georg Brandl | fbd1b22 | 2009-12-29 21:38:35 +0000 | [diff] [blame] | 96 | The following exceptions are used mostly as base classes for other exceptions. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 97 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 98 | .. exception:: BaseException |
| 99 | |
| 100 | The base class for all built-in exceptions. It is not meant to be directly |
| Georg Brandl | fb6fd5d | 2011-01-07 18:28:45 +0000 | [diff] [blame] | 101 | inherited by user-defined classes (for that, use :exc:`Exception`). If |
| Amaury Forgeot d'Arc | 3b1acf1 | 2011-11-22 19:34:08 +0100 | [diff] [blame] | 102 | :func:`str` is called on an instance of this class, the representation of |
| 103 | the argument(s) to the instance are returned, or the empty string when |
| 104 | there were no arguments. |
| Georg Brandl | fb6fd5d | 2011-01-07 18:28:45 +0000 | [diff] [blame] | 105 | |
| 106 | .. attribute:: args |
| 107 | |
| 108 | The tuple of arguments given to the exception constructor. Some built-in |
| Andrew Svetlov | 5898d4f | 2014-04-01 00:44:13 +0300 | [diff] [blame] | 109 | exceptions (like :exc:`OSError`) expect a certain number of arguments and |
| Georg Brandl | fb6fd5d | 2011-01-07 18:28:45 +0000 | [diff] [blame] | 110 | assign a special meaning to the elements of this tuple, while others are |
| 111 | usually called only with a single string giving an error message. |
| 112 | |
| 113 | .. method:: with_traceback(tb) |
| 114 | |
| 115 | This method sets *tb* as the new traceback for the exception and returns |
| Irit Katriel | c590c23 | 2020-12-16 16:03:32 +0000 | [diff] [blame] | 116 | the exception object. It was more commonly used before the exception |
| 117 | chaining features of :pep:`3134` became available. The following example |
| 118 | shows how we can convert an instance of ``SomeException`` into an |
| 119 | instance of ``OtherException`` while preserving the traceback. Once |
| 120 | raised, the current frame is pushed onto the traceback of the |
| 121 | ``OtherException``, as would have happened to the traceback of the |
| Miss Islington (bot) | 6275ea0 | 2021-05-12 17:27:08 -0700 | [diff] [blame] | 122 | original ``SomeException`` had we allowed it to propagate to the caller. :: |
| Georg Brandl | fb6fd5d | 2011-01-07 18:28:45 +0000 | [diff] [blame] | 123 | |
| 124 | try: |
| 125 | ... |
| 126 | except SomeException: |
| 127 | tb = sys.exc_info()[2] |
| 128 | raise OtherException(...).with_traceback(tb) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 129 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 130 | |
| 131 | .. exception:: Exception |
| 132 | |
| 133 | All built-in, non-system-exiting exceptions are derived from this class. All |
| 134 | user-defined exceptions should also be derived from this class. |
| 135 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 136 | |
| 137 | .. exception:: ArithmeticError |
| 138 | |
| 139 | The base class for those built-in exceptions that are raised for various |
| 140 | arithmetic errors: :exc:`OverflowError`, :exc:`ZeroDivisionError`, |
| 141 | :exc:`FloatingPointError`. |
| 142 | |
| 143 | |
| Georg Brandl | 0bdfbfa | 2010-12-18 17:51:28 +0000 | [diff] [blame] | 144 | .. exception:: BufferError |
| 145 | |
| 146 | Raised when a :ref:`buffer <bufferobjects>` related operation cannot be |
| 147 | performed. |
| 148 | |
| 149 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 150 | .. exception:: LookupError |
| 151 | |
| Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 152 | The base class for the exceptions that are raised when a key or index used on |
| 153 | a mapping or sequence is invalid: :exc:`IndexError`, :exc:`KeyError`. This |
| 154 | can be raised directly by :func:`codecs.lookup`. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 155 | |
| 156 | |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 157 | Concrete exceptions |
| 158 | ------------------- |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 159 | |
| Georg Brandl | fbd1b22 | 2009-12-29 21:38:35 +0000 | [diff] [blame] | 160 | The following exceptions are the exceptions that are usually raised. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 161 | |
| 162 | .. exception:: AssertionError |
| 163 | |
| 164 | .. index:: statement: assert |
| 165 | |
| 166 | Raised when an :keyword:`assert` statement fails. |
| 167 | |
| 168 | |
| 169 | .. exception:: AttributeError |
| 170 | |
| Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 171 | Raised when an attribute reference (see :ref:`attribute-references`) or |
| 172 | assignment fails. (When an object does not support attribute references or |
| 173 | attribute assignments at all, :exc:`TypeError` is raised.) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 174 | |
| Pablo Galindo | 37494b4 | 2021-04-14 02:36:07 +0100 | [diff] [blame] | 175 | The :attr:`name` and :attr:`obj` attributes can be set using keyword-only |
| 176 | arguments to the constructor. When set they represent the name of the attribute |
| 177 | that was attempted to be accessed and the object that was accessed for said |
| 178 | attribute, respectively. |
| 179 | |
| 180 | .. versionchanged:: 3.10 |
| 181 | Added the :attr:`name` and :attr:`obj` attributes. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 182 | |
| 183 | .. exception:: EOFError |
| 184 | |
| Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 185 | Raised when the :func:`input` function hits an end-of-file condition (EOF) |
| 186 | without reading any data. (N.B.: the :meth:`io.IOBase.read` and |
| 187 | :meth:`io.IOBase.readline` methods return an empty string when they hit EOF.) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 188 | |
| 189 | |
| 190 | .. exception:: FloatingPointError |
| 191 | |
| Nathaniel J. Smith | 735ae8d | 2018-01-05 23:15:34 -0800 | [diff] [blame] | 192 | Not currently used. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 193 | |
| 194 | |
| 195 | .. exception:: GeneratorExit |
| 196 | |
| Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 197 | Raised when a :term:`generator` or :term:`coroutine` is closed; |
| 198 | see :meth:`generator.close` and :meth:`coroutine.close`. It |
| Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 199 | directly inherits from :exc:`BaseException` instead of :exc:`Exception` since |
| 200 | it is technically not an error. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 201 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 202 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 203 | .. exception:: ImportError |
| 204 | |
| Eric Snow | c943265 | 2016-09-07 15:42:32 -0700 | [diff] [blame] | 205 | Raised when the :keyword:`import` statement has troubles trying to |
| 206 | load a module. Also raised when the "from list" in ``from ... import`` |
| 207 | has a name that cannot be found. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 208 | |
| Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 209 | The :attr:`name` and :attr:`path` attributes can be set using keyword-only |
| 210 | arguments to the constructor. When set they represent the name of the module |
| 211 | that was attempted to be imported and the path to any file which triggered |
| 212 | the exception, respectively. |
| 213 | |
| 214 | .. versionchanged:: 3.3 |
| 215 | Added the :attr:`name` and :attr:`path` attributes. |
| 216 | |
| Eric Snow | c943265 | 2016-09-07 15:42:32 -0700 | [diff] [blame] | 217 | .. exception:: ModuleNotFoundError |
| 218 | |
| 219 | A subclass of :exc:`ImportError` which is raised by :keyword:`import` |
| 220 | when a module could not be located. It is also raised when ``None`` |
| 221 | is found in :data:`sys.modules`. |
| 222 | |
| 223 | .. versionadded:: 3.6 |
| 224 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 225 | |
| 226 | .. exception:: IndexError |
| 227 | |
| Georg Brandl | 95817b3 | 2008-05-11 14:30:18 +0000 | [diff] [blame] | 228 | Raised when a sequence subscript is out of range. (Slice indices are |
| 229 | silently truncated to fall in the allowed range; if an index is not an |
| 230 | integer, :exc:`TypeError` is raised.) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 231 | |
| Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 232 | .. XXX xref to sequences |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 233 | |
| 234 | |
| 235 | .. exception:: KeyError |
| 236 | |
| 237 | Raised when a mapping (dictionary) key is not found in the set of existing keys. |
| 238 | |
| Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 239 | .. XXX xref to mapping objects? |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 240 | |
| 241 | |
| 242 | .. exception:: KeyboardInterrupt |
| 243 | |
| 244 | Raised when the user hits the interrupt key (normally :kbd:`Control-C` or |
| Georg Brandl | 81ac1ce | 2007-08-31 17:17:17 +0000 | [diff] [blame] | 245 | :kbd:`Delete`). During execution, a check for interrupts is made |
| 246 | regularly. The exception inherits from :exc:`BaseException` so as to not be |
| 247 | accidentally caught by code that catches :exc:`Exception` and thus prevent |
| 248 | the interpreter from exiting. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 249 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 250 | |
| 251 | .. exception:: MemoryError |
| 252 | |
| 253 | Raised when an operation runs out of memory but the situation may still be |
| 254 | rescued (by deleting some objects). The associated value is a string indicating |
| 255 | what kind of (internal) operation ran out of memory. Note that because of the |
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 256 | underlying memory management architecture (C's :c:func:`malloc` function), the |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 257 | interpreter may not always be able to completely recover from this situation; it |
| 258 | nevertheless raises an exception so that a stack traceback can be printed, in |
| 259 | case a run-away program was the cause. |
| 260 | |
| 261 | |
| 262 | .. exception:: NameError |
| 263 | |
| 264 | Raised when a local or global name is not found. This applies only to |
| 265 | unqualified names. The associated value is an error message that includes the |
| 266 | name that could not be found. |
| 267 | |
| Pablo Galindo | 5bf8bf2 | 2021-04-14 15:10:33 +0100 | [diff] [blame] | 268 | The :attr:`name` attribute can be set using a keyword-only argument to the |
| 269 | constructor. When set it represent the name of the variable that was attempted |
| 270 | to be accessed. |
| 271 | |
| 272 | .. versionchanged:: 3.10 |
| 273 | Added the :attr:`name` attribute. |
| 274 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 275 | |
| 276 | .. exception:: NotImplementedError |
| 277 | |
| 278 | This exception is derived from :exc:`RuntimeError`. In user defined base |
| Ethan Furman | 20bd9f0 | 2016-08-05 15:10:16 -0700 | [diff] [blame] | 279 | classes, abstract methods should raise this exception when they require |
| 280 | derived classes to override the method, or while the class is being |
| 281 | developed to indicate that the real implementation still needs to be added. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 282 | |
| Ethan Furman | 20bd9f0 | 2016-08-05 15:10:16 -0700 | [diff] [blame] | 283 | .. note:: |
| 284 | |
| delirious-lettuce | 3378b20 | 2017-05-19 14:37:57 -0600 | [diff] [blame] | 285 | It should not be used to indicate that an operator or method is not |
| Ethan Furman | 20bd9f0 | 2016-08-05 15:10:16 -0700 | [diff] [blame] | 286 | meant to be supported at all -- in that case either leave the operator / |
| 287 | method undefined or, if a subclass, set it to :data:`None`. |
| 288 | |
| 289 | .. note:: |
| 290 | |
| 291 | ``NotImplementedError`` and ``NotImplemented`` are not interchangeable, |
| 292 | even though they have similar names and purposes. See |
| 293 | :data:`NotImplemented` for details on when to use it. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 294 | |
| Martin Panter | 5487c13 | 2015-10-26 11:05:42 +0000 | [diff] [blame] | 295 | .. exception:: OSError([arg]) |
| 296 | OSError(errno, strerror[, filename[, winerror[, filename2]]]) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 297 | |
| Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 298 | .. index:: module: errno |
| 299 | |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 300 | This exception is raised when a system function returns a system-related |
| 301 | error, including I/O failures such as "file not found" or "disk full" |
| Martin Panter | 5487c13 | 2015-10-26 11:05:42 +0000 | [diff] [blame] | 302 | (not for illegal argument types or other incidental errors). |
| Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 303 | |
| Martin Panter | 5487c13 | 2015-10-26 11:05:42 +0000 | [diff] [blame] | 304 | The second form of the constructor sets the corresponding attributes, |
| 305 | described below. The attributes default to :const:`None` if not |
| 306 | specified. For backwards compatibility, if three arguments are passed, |
| 307 | the :attr:`~BaseException.args` attribute contains only a 2-tuple |
| 308 | of the first two constructor arguments. |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 309 | |
| Martin Panter | 5487c13 | 2015-10-26 11:05:42 +0000 | [diff] [blame] | 310 | The constructor often actually returns a subclass of :exc:`OSError`, as |
| 311 | described in `OS exceptions`_ below. The particular subclass depends on |
| 312 | the final :attr:`.errno` value. This behaviour only occurs when |
| 313 | constructing :exc:`OSError` directly or via an alias, and is not |
| 314 | inherited when subclassing. |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 315 | |
| Martin Panter | 5487c13 | 2015-10-26 11:05:42 +0000 | [diff] [blame] | 316 | .. attribute:: errno |
| 317 | |
| 318 | A numeric error code from the C variable :c:data:`errno`. |
| 319 | |
| 320 | .. attribute:: winerror |
| 321 | |
| 322 | Under Windows, this gives you the native |
| 323 | Windows error code. The :attr:`.errno` attribute is then an approximate |
| 324 | translation, in POSIX terms, of that native error code. |
| 325 | |
| 326 | Under Windows, if the *winerror* constructor argument is an integer, |
| 327 | the :attr:`.errno` attribute is determined from the Windows error code, |
| 328 | and the *errno* argument is ignored. On other platforms, the |
| 329 | *winerror* argument is ignored, and the :attr:`winerror` attribute |
| 330 | does not exist. |
| 331 | |
| 332 | .. attribute:: strerror |
| 333 | |
| 334 | The corresponding error message, as provided by |
| 335 | the operating system. It is formatted by the C |
| 336 | functions :c:func:`perror` under POSIX, and :c:func:`FormatMessage` |
| 337 | under Windows. |
| 338 | |
| 339 | .. attribute:: filename |
| 340 | filename2 |
| 341 | |
| 342 | For exceptions that involve a file system path (such as :func:`open` or |
| 343 | :func:`os.unlink`), :attr:`filename` is the file name passed to the function. |
| 344 | For functions that involve two file system paths (such as |
| 345 | :func:`os.rename`), :attr:`filename2` corresponds to the second |
| 346 | file name passed to the function. |
| Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 347 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 348 | |
| Antoine Pitrou | 195e702 | 2011-10-12 16:46:46 +0200 | [diff] [blame] | 349 | .. versionchanged:: 3.3 |
| 350 | :exc:`EnvironmentError`, :exc:`IOError`, :exc:`WindowsError`, |
| Berker Peksag | 8fafc74 | 2016-04-11 12:23:04 +0300 | [diff] [blame] | 351 | :exc:`socket.error`, :exc:`select.error` and |
| Martin Panter | 5487c13 | 2015-10-26 11:05:42 +0000 | [diff] [blame] | 352 | :exc:`mmap.error` have been merged into :exc:`OSError`, and the |
| 353 | constructor may return a subclass. |
| Antoine Pitrou | 195e702 | 2011-10-12 16:46:46 +0200 | [diff] [blame] | 354 | |
| Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 355 | .. versionchanged:: 3.4 |
| Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 356 | The :attr:`filename` attribute is now the original file name passed to |
| 357 | the function, instead of the name encoded to or decoded from the |
| Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame] | 358 | :term:`filesystem encoding and error handler`. Also, the *filename2* |
| 359 | constructor argument and attribute was added. |
| Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 360 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 361 | |
| 362 | .. exception:: OverflowError |
| 363 | |
| 364 | Raised when the result of an arithmetic operation is too large to be |
| Georg Brandl | ba956ae | 2007-11-29 17:24:34 +0000 | [diff] [blame] | 365 | represented. This cannot occur for integers (which would rather raise |
| Terry Jan Reedy | b6d1f48 | 2014-06-16 03:31:00 -0400 | [diff] [blame] | 366 | :exc:`MemoryError` than give up). However, for historical reasons, |
| 367 | OverflowError is sometimes raised for integers that are outside a required |
| 368 | range. Because of the lack of standardization of floating point exception |
| 369 | handling in C, most floating point operations are not checked. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 370 | |
| 371 | |
| Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 372 | .. exception:: RecursionError |
| 373 | |
| 374 | This exception is derived from :exc:`RuntimeError`. It is raised when the |
| 375 | interpreter detects that the maximum recursion depth (see |
| 376 | :func:`sys.getrecursionlimit`) is exceeded. |
| 377 | |
| 378 | .. versionadded:: 3.5 |
| 379 | Previously, a plain :exc:`RuntimeError` was raised. |
| 380 | |
| 381 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 382 | .. exception:: ReferenceError |
| 383 | |
| 384 | This exception is raised when a weak reference proxy, created by the |
| 385 | :func:`weakref.proxy` function, is used to access an attribute of the referent |
| 386 | after it has been garbage collected. For more information on weak references, |
| 387 | see the :mod:`weakref` module. |
| 388 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 389 | |
| 390 | .. exception:: RuntimeError |
| 391 | |
| 392 | Raised when an error is detected that doesn't fall in any of the other |
| 393 | categories. The associated value is a string indicating what precisely went |
| Antoine Pitrou | 9527f16 | 2013-11-25 19:08:32 +0100 | [diff] [blame] | 394 | wrong. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 395 | |
| 396 | |
| 397 | .. exception:: StopIteration |
| 398 | |
| Georg Brandl | c4a55fc | 2010-02-06 18:46:57 +0000 | [diff] [blame] | 399 | Raised by built-in function :func:`next` and an :term:`iterator`\'s |
| Ezio Melotti | 1dd7c30 | 2012-10-12 13:45:38 +0300 | [diff] [blame] | 400 | :meth:`~iterator.__next__` method to signal that there are no further |
| 401 | items produced by the iterator. |
| Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 402 | |
| 403 | The exception object has a single attribute :attr:`value`, which is |
| 404 | given as an argument when constructing the exception, and defaults |
| 405 | to :const:`None`. |
| 406 | |
| Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 407 | When a :term:`generator` or :term:`coroutine` function |
| 408 | returns, a new :exc:`StopIteration` instance is |
| Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 409 | raised, and the value returned by the function is used as the |
| 410 | :attr:`value` parameter to the constructor of the exception. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 411 | |
| Yury Selivanov | 43c47fe | 2018-01-26 15:24:24 -0500 | [diff] [blame] | 412 | If a generator code directly or indirectly raises :exc:`StopIteration`, |
| 413 | it is converted into a :exc:`RuntimeError` (retaining the |
| 414 | :exc:`StopIteration` as the new exception's cause). |
| Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 415 | |
| Nick Coghlan | 0ed8019 | 2012-01-14 14:43:24 +1000 | [diff] [blame] | 416 | .. versionchanged:: 3.3 |
| 417 | Added ``value`` attribute and the ability for generator functions to |
| 418 | use it to return a value. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 419 | |
| Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 420 | .. versionchanged:: 3.5 |
| Yury Selivanov | 43c47fe | 2018-01-26 15:24:24 -0500 | [diff] [blame] | 421 | Introduced the RuntimeError transformation via |
| 422 | ``from __future__ import generator_stop``, see :pep:`479`. |
| 423 | |
| 424 | .. versionchanged:: 3.7 |
| 425 | Enable :pep:`479` for all code by default: a :exc:`StopIteration` |
| 426 | error raised in a generator is transformed into a :exc:`RuntimeError`. |
| Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 427 | |
| Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 428 | .. exception:: StopAsyncIteration |
| 429 | |
| 430 | Must be raised by :meth:`__anext__` method of an |
| 431 | :term:`asynchronous iterator` object to stop the iteration. |
| 432 | |
| 433 | .. versionadded:: 3.5 |
| Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 434 | |
| Miss Islington (bot) | 2af690f | 2021-06-06 19:09:34 -0700 | [diff] [blame] | 435 | .. exception:: SyntaxError(message, details) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 436 | |
| 437 | Raised when the parser encounters a syntax error. This may occur in an |
| Miss Islington (bot) | 2af690f | 2021-06-06 19:09:34 -0700 | [diff] [blame] | 438 | :keyword:`import` statement, in a call to the built-in functions |
| 439 | :func:`compile`, :func:`exec`, |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 440 | or :func:`eval`, or when reading the initial script or standard input |
| 441 | (also interactively). |
| 442 | |
| Ammar Askar | b2a91e0 | 2021-04-02 17:25:31 -0400 | [diff] [blame] | 443 | The :func:`str` of the exception instance returns only the error message. |
| Miss Islington (bot) | 2af690f | 2021-06-06 19:09:34 -0700 | [diff] [blame] | 444 | Details is a tuple whose members are also available as separate attributes. |
| Ammar Askar | b2a91e0 | 2021-04-02 17:25:31 -0400 | [diff] [blame] | 445 | |
| 446 | .. attribute:: filename |
| 447 | |
| 448 | The name of the file the syntax error occurred in. |
| 449 | |
| 450 | .. attribute:: lineno |
| 451 | |
| 452 | Which line number in the file the error occurred in. This is |
| 453 | 1-indexed: the first line in the file has a ``lineno`` of 1. |
| 454 | |
| 455 | .. attribute:: offset |
| 456 | |
| 457 | The column in the line where the error occurred. This is |
| 458 | 1-indexed: the first character in the line has an ``offset`` of 1. |
| 459 | |
| 460 | .. attribute:: text |
| 461 | |
| 462 | The source code text involved in the error. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 463 | |
| Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 464 | .. attribute:: end_lineno |
| 465 | |
| 466 | Which line number in the file the error occurred ends in. This is |
| 467 | 1-indexed: the first line in the file has a ``lineno`` of 1. |
| 468 | |
| 469 | .. attribute:: end_offset |
| 470 | |
| 471 | The column in the end line where the error occurred finishes. This is |
| 472 | 1-indexed: the first character in the line has an ``offset`` of 1. |
| 473 | |
| Miss Islington (bot) | 2af690f | 2021-06-06 19:09:34 -0700 | [diff] [blame] | 474 | For errors in f-string fields, the message is prefixed by "f-string: " |
| 475 | and the offsets are offsets in a text constructed from the replacement |
| 476 | expression. For example, compiling f'Bad {a b} field' results in this |
| 477 | args attribute: ('f-string: ...', ('', 1, 2, '(a b)\n', 1, 5)). |
| 478 | |
| Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 479 | .. versionchanged:: 3.10 |
| 480 | Added the :attr:`end_lineno` and :attr:`end_offset` attributes. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 481 | |
| Georg Brandl | 0bdfbfa | 2010-12-18 17:51:28 +0000 | [diff] [blame] | 482 | .. exception:: IndentationError |
| 483 | |
| 484 | Base class for syntax errors related to incorrect indentation. This is a |
| 485 | subclass of :exc:`SyntaxError`. |
| 486 | |
| 487 | |
| 488 | .. exception:: TabError |
| 489 | |
| 490 | Raised when indentation contains an inconsistent use of tabs and spaces. |
| 491 | This is a subclass of :exc:`IndentationError`. |
| 492 | |
| 493 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 494 | .. exception:: SystemError |
| 495 | |
| 496 | Raised when the interpreter finds an internal error, but the situation does not |
| 497 | look so serious to cause it to abandon all hope. The associated value is a |
| 498 | string indicating what went wrong (in low-level terms). |
| 499 | |
| 500 | You should report this to the author or maintainer of your Python interpreter. |
| 501 | Be sure to report the version of the Python interpreter (``sys.version``; it is |
| 502 | also printed at the start of an interactive Python session), the exact error |
| 503 | message (the exception's associated value) and if possible the source of the |
| 504 | program that triggered the error. |
| 505 | |
| 506 | |
| 507 | .. exception:: SystemExit |
| 508 | |
| Berker Peksag | 77a6b20 | 2015-03-10 14:47:15 +0200 | [diff] [blame] | 509 | This exception is raised by the :func:`sys.exit` function. It inherits from |
| 510 | :exc:`BaseException` instead of :exc:`Exception` so that it is not accidentally |
| 511 | caught by code that catches :exc:`Exception`. This allows the exception to |
| 512 | properly propagate up and cause the interpreter to exit. When it is not |
| 513 | handled, the Python interpreter exits; no stack traceback is printed. The |
| 514 | constructor accepts the same optional argument passed to :func:`sys.exit`. |
| 515 | If the value is an integer, it specifies the system exit status (passed to |
| 516 | C's :c:func:`exit` function); if it is ``None``, the exit status is zero; if |
| 517 | it has another type (such as a string), the object's value is printed and |
| Georg Brandl | 95817b3 | 2008-05-11 14:30:18 +0000 | [diff] [blame] | 518 | the exit status is one. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 519 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 520 | A call to :func:`sys.exit` is translated into an exception so that clean-up |
| 521 | handlers (:keyword:`finally` clauses of :keyword:`try` statements) can be |
| 522 | executed, and so that a debugger can execute a script without running the risk |
| 523 | of losing control. The :func:`os._exit` function can be used if it is |
| 524 | absolutely positively necessary to exit immediately (for example, in the child |
| Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 525 | process after a call to :func:`os.fork`). |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 526 | |
| Berker Peksag | 77a6b20 | 2015-03-10 14:47:15 +0200 | [diff] [blame] | 527 | .. attribute:: code |
| 528 | |
| 529 | The exit status or error message that is passed to the constructor. |
| 530 | (Defaults to ``None``.) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 531 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 532 | |
| 533 | .. exception:: TypeError |
| 534 | |
| 535 | Raised when an operation or function is applied to an object of inappropriate |
| 536 | type. The associated value is a string giving details about the type mismatch. |
| 537 | |
| Ethan Furman | 20bd9f0 | 2016-08-05 15:10:16 -0700 | [diff] [blame] | 538 | This exception may be raised by user code to indicate that an attempted |
| 539 | operation on an object is not supported, and is not meant to be. If an object |
| 540 | is meant to support a given operation but has not yet provided an |
| 541 | implementation, :exc:`NotImplementedError` is the proper exception to raise. |
| 542 | |
| 543 | Passing arguments of the wrong type (e.g. passing a :class:`list` when an |
| 544 | :class:`int` is expected) should result in a :exc:`TypeError`, but passing |
| 545 | arguments with the wrong value (e.g. a number outside expected boundaries) |
| 546 | should result in a :exc:`ValueError`. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 547 | |
| 548 | .. exception:: UnboundLocalError |
| 549 | |
| 550 | Raised when a reference is made to a local variable in a function or method, but |
| 551 | no value has been bound to that variable. This is a subclass of |
| 552 | :exc:`NameError`. |
| 553 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 554 | |
| 555 | .. exception:: UnicodeError |
| 556 | |
| 557 | Raised when a Unicode-related encoding or decoding error occurs. It is a |
| 558 | subclass of :exc:`ValueError`. |
| 559 | |
| Benjamin Peterson | 78f7e3a | 2012-12-02 11:33:06 -0500 | [diff] [blame] | 560 | :exc:`UnicodeError` has attributes that describe the encoding or decoding |
| 561 | error. For example, ``err.object[err.start:err.end]`` gives the particular |
| 562 | invalid input that the codec failed on. |
| 563 | |
| 564 | .. attribute:: encoding |
| 565 | |
| 566 | The name of the encoding that raised the error. |
| 567 | |
| 568 | .. attribute:: reason |
| 569 | |
| 570 | A string describing the specific codec error. |
| 571 | |
| 572 | .. attribute:: object |
| 573 | |
| 574 | The object the codec was attempting to encode or decode. |
| 575 | |
| 576 | .. attribute:: start |
| 577 | |
| 578 | The first index of invalid data in :attr:`object`. |
| 579 | |
| 580 | .. attribute:: end |
| 581 | |
| 582 | The index after the last invalid data in :attr:`object`. |
| 583 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 584 | |
| 585 | .. exception:: UnicodeEncodeError |
| 586 | |
| 587 | Raised when a Unicode-related error occurs during encoding. It is a subclass of |
| 588 | :exc:`UnicodeError`. |
| 589 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 590 | |
| 591 | .. exception:: UnicodeDecodeError |
| 592 | |
| 593 | Raised when a Unicode-related error occurs during decoding. It is a subclass of |
| 594 | :exc:`UnicodeError`. |
| 595 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 596 | |
| 597 | .. exception:: UnicodeTranslateError |
| 598 | |
| 599 | Raised when a Unicode-related error occurs during translating. It is a subclass |
| 600 | of :exc:`UnicodeError`. |
| 601 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 602 | |
| 603 | .. exception:: ValueError |
| 604 | |
| Raymond Hettinger | feabae9 | 2018-07-17 08:35:26 -0700 | [diff] [blame] | 605 | Raised when an operation or function receives an argument that has the |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 606 | right type but an inappropriate value, and the situation is not described by a |
| 607 | more precise exception such as :exc:`IndexError`. |
| 608 | |
| 609 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 610 | .. exception:: ZeroDivisionError |
| 611 | |
| 612 | Raised when the second argument of a division or modulo operation is zero. The |
| 613 | associated value is a string indicating the type of the operands and the |
| 614 | operation. |
| 615 | |
| Georg Brandl | fbd1b22 | 2009-12-29 21:38:35 +0000 | [diff] [blame] | 616 | |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 617 | The following exceptions are kept for compatibility with previous versions; |
| 618 | starting from Python 3.3, they are aliases of :exc:`OSError`. |
| 619 | |
| 620 | .. exception:: EnvironmentError |
| 621 | |
| 622 | .. exception:: IOError |
| 623 | |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 624 | .. exception:: WindowsError |
| 625 | |
| 626 | Only available on Windows. |
| 627 | |
| 628 | |
| 629 | OS exceptions |
| 630 | ^^^^^^^^^^^^^ |
| 631 | |
| 632 | The following exceptions are subclasses of :exc:`OSError`, they get raised |
| 633 | depending on the system error code. |
| 634 | |
| 635 | .. exception:: BlockingIOError |
| 636 | |
| 637 | Raised when an operation would block on an object (e.g. socket) set |
| 638 | for non-blocking operation. |
| 639 | Corresponds to :c:data:`errno` ``EAGAIN``, ``EALREADY``, |
| 640 | ``EWOULDBLOCK`` and ``EINPROGRESS``. |
| 641 | |
| Antoine Pitrou | f55011f | 2011-10-12 18:57:23 +0200 | [diff] [blame] | 642 | In addition to those of :exc:`OSError`, :exc:`BlockingIOError` can have |
| 643 | one more attribute: |
| 644 | |
| 645 | .. attribute:: characters_written |
| 646 | |
| 647 | An integer containing the number of characters written to the stream |
| 648 | before it blocked. This attribute is available when using the |
| 649 | buffered I/O classes from the :mod:`io` module. |
| 650 | |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 651 | .. exception:: ChildProcessError |
| 652 | |
| 653 | Raised when an operation on a child process failed. |
| 654 | Corresponds to :c:data:`errno` ``ECHILD``. |
| 655 | |
| 656 | .. exception:: ConnectionError |
| 657 | |
| Ezio Melotti | b24d3cf | 2012-10-21 03:22:05 +0300 | [diff] [blame] | 658 | A base class for connection-related issues. |
| 659 | |
| 660 | Subclasses are :exc:`BrokenPipeError`, :exc:`ConnectionAbortedError`, |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 661 | :exc:`ConnectionRefusedError` and :exc:`ConnectionResetError`. |
| 662 | |
| Ezio Melotti | b24d3cf | 2012-10-21 03:22:05 +0300 | [diff] [blame] | 663 | .. exception:: BrokenPipeError |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 664 | |
| Ezio Melotti | b24d3cf | 2012-10-21 03:22:05 +0300 | [diff] [blame] | 665 | A subclass of :exc:`ConnectionError`, raised when trying to write on a |
| 666 | pipe while the other end has been closed, or trying to write on a socket |
| 667 | which has been shutdown for writing. |
| 668 | Corresponds to :c:data:`errno` ``EPIPE`` and ``ESHUTDOWN``. |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 669 | |
| Ezio Melotti | b24d3cf | 2012-10-21 03:22:05 +0300 | [diff] [blame] | 670 | .. exception:: ConnectionAbortedError |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 671 | |
| Ezio Melotti | b24d3cf | 2012-10-21 03:22:05 +0300 | [diff] [blame] | 672 | A subclass of :exc:`ConnectionError`, raised when a connection attempt |
| 673 | is aborted by the peer. |
| 674 | Corresponds to :c:data:`errno` ``ECONNABORTED``. |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 675 | |
| Ezio Melotti | b24d3cf | 2012-10-21 03:22:05 +0300 | [diff] [blame] | 676 | .. exception:: ConnectionRefusedError |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 677 | |
| Ezio Melotti | b24d3cf | 2012-10-21 03:22:05 +0300 | [diff] [blame] | 678 | A subclass of :exc:`ConnectionError`, raised when a connection attempt |
| 679 | is refused by the peer. |
| 680 | Corresponds to :c:data:`errno` ``ECONNREFUSED``. |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 681 | |
| Ezio Melotti | b24d3cf | 2012-10-21 03:22:05 +0300 | [diff] [blame] | 682 | .. exception:: ConnectionResetError |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 683 | |
| Ezio Melotti | b24d3cf | 2012-10-21 03:22:05 +0300 | [diff] [blame] | 684 | A subclass of :exc:`ConnectionError`, raised when a connection is |
| 685 | reset by the peer. |
| 686 | Corresponds to :c:data:`errno` ``ECONNRESET``. |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 687 | |
| 688 | .. exception:: FileExistsError |
| 689 | |
| 690 | Raised when trying to create a file or directory which already exists. |
| 691 | Corresponds to :c:data:`errno` ``EEXIST``. |
| 692 | |
| 693 | .. exception:: FileNotFoundError |
| 694 | |
| 695 | Raised when a file or directory is requested but doesn't exist. |
| 696 | Corresponds to :c:data:`errno` ``ENOENT``. |
| 697 | |
| 698 | .. exception:: InterruptedError |
| 699 | |
| 700 | Raised when a system call is interrupted by an incoming signal. |
| Victor Stinner | f70e1ca | 2015-03-30 21:16:11 +0200 | [diff] [blame] | 701 | Corresponds to :c:data:`errno` :py:data:`~errno.EINTR`. |
| 702 | |
| 703 | .. versionchanged:: 3.5 |
| 704 | Python now retries system calls when a syscall is interrupted by a |
| 705 | signal, except if the signal handler raises an exception (see :pep:`475` |
| 706 | for the rationale), instead of raising :exc:`InterruptedError`. |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 707 | |
| 708 | .. exception:: IsADirectoryError |
| 709 | |
| 710 | Raised when a file operation (such as :func:`os.remove`) is requested |
| 711 | on a directory. |
| 712 | Corresponds to :c:data:`errno` ``EISDIR``. |
| 713 | |
| 714 | .. exception:: NotADirectoryError |
| 715 | |
| Miss Islington (bot) | 84494db | 2021-08-03 05:05:04 -0700 | [diff] [blame] | 716 | Raised when a directory operation (such as :func:`os.listdir`) is requested on |
| 717 | something which is not a directory. On most POSIX platforms, it may also be |
| 718 | raised if an operation attempts to open or traverse a non-directory file as if |
| 719 | it were a directory. |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 720 | Corresponds to :c:data:`errno` ``ENOTDIR``. |
| 721 | |
| 722 | .. exception:: PermissionError |
| 723 | |
| 724 | Raised when trying to run an operation without the adequate access |
| 725 | rights - for example filesystem permissions. |
| 726 | Corresponds to :c:data:`errno` ``EACCES`` and ``EPERM``. |
| 727 | |
| 728 | .. exception:: ProcessLookupError |
| 729 | |
| 730 | Raised when a given process doesn't exist. |
| 731 | Corresponds to :c:data:`errno` ``ESRCH``. |
| 732 | |
| 733 | .. exception:: TimeoutError |
| 734 | |
| 735 | Raised when a system function timed out at the system level. |
| 736 | Corresponds to :c:data:`errno` ``ETIMEDOUT``. |
| 737 | |
| 738 | .. versionadded:: 3.3 |
| 739 | All the above :exc:`OSError` subclasses were added. |
| 740 | |
| 741 | |
| 742 | .. seealso:: |
| 743 | |
| 744 | :pep:`3151` - Reworking the OS and IO exception hierarchy |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 745 | |
| 746 | |
| Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 747 | .. _warning-categories-as-exceptions: |
| 748 | |
| Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 749 | Warnings |
| 750 | -------- |
| 751 | |
| Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 752 | The following exceptions are used as warning categories; see the |
| 753 | :ref:`warning-categories` documentation for more details. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 754 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 755 | .. exception:: Warning |
| 756 | |
| 757 | Base class for warning categories. |
| 758 | |
| 759 | |
| 760 | .. exception:: UserWarning |
| 761 | |
| 762 | Base class for warnings generated by user code. |
| 763 | |
| 764 | |
| 765 | .. exception:: DeprecationWarning |
| 766 | |
| Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 767 | Base class for warnings about deprecated features when those warnings are |
| 768 | intended for other Python developers. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 769 | |
| Victor Stinner | b9783d2 | 2020-01-24 10:22:18 +0100 | [diff] [blame] | 770 | Ignored by the default warning filters, except in the ``__main__`` module |
| 771 | (:pep:`565`). Enabling the :ref:`Python Development Mode <devmode>` shows |
| 772 | this warning. |
| 773 | |
| Łukasz Langa | 2184bc7 | 2021-09-02 18:18:47 +0200 | [diff] [blame] | 774 | The deprecation policy is described in :pep:`387`. |
| 775 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 776 | |
| 777 | .. exception:: PendingDeprecationWarning |
| 778 | |
| Inada Naoki | a3283ef | 2019-04-15 21:40:23 +0900 | [diff] [blame] | 779 | Base class for warnings about features which are obsolete and |
| 780 | expected to be deprecated in the future, but are not deprecated |
| 781 | at the moment. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 782 | |
| Inada Naoki | a3283ef | 2019-04-15 21:40:23 +0900 | [diff] [blame] | 783 | This class is rarely used as emitting a warning about a possible |
| 784 | upcoming deprecation is unusual, and :exc:`DeprecationWarning` |
| 785 | is preferred for already active deprecations. |
| Inada Naoki | 176d263 | 2019-04-05 17:54:24 +0900 | [diff] [blame] | 786 | |
| Victor Stinner | b9783d2 | 2020-01-24 10:22:18 +0100 | [diff] [blame] | 787 | Ignored by the default warning filters. Enabling the :ref:`Python |
| 788 | Development Mode <devmode>` shows this warning. |
| 789 | |
| Łukasz Langa | 2184bc7 | 2021-09-02 18:18:47 +0200 | [diff] [blame] | 790 | The deprecation policy is described in :pep:`387`. |
| 791 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 792 | |
| 793 | .. exception:: SyntaxWarning |
| 794 | |
| Martin Panter | d21e0b5 | 2015-10-10 10:36:22 +0000 | [diff] [blame] | 795 | Base class for warnings about dubious syntax. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 796 | |
| 797 | |
| 798 | .. exception:: RuntimeWarning |
| 799 | |
| 800 | Base class for warnings about dubious runtime behavior. |
| 801 | |
| 802 | |
| 803 | .. exception:: FutureWarning |
| 804 | |
| Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 805 | Base class for warnings about deprecated features when those warnings are |
| 806 | intended for end users of applications that are written in Python. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 807 | |
| 808 | |
| 809 | .. exception:: ImportWarning |
| 810 | |
| 811 | Base class for warnings about probable mistakes in module imports. |
| 812 | |
| Victor Stinner | b9783d2 | 2020-01-24 10:22:18 +0100 | [diff] [blame] | 813 | Ignored by the default warning filters. Enabling the :ref:`Python |
| 814 | Development Mode <devmode>` shows this warning. |
| 815 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 816 | |
| 817 | .. exception:: UnicodeWarning |
| 818 | |
| 819 | Base class for warnings related to Unicode. |
| 820 | |
| Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 821 | |
| Inada Naoki | 4827483 | 2021-03-29 12:28:14 +0900 | [diff] [blame] | 822 | .. exception:: EncodingWarning |
| 823 | |
| 824 | Base class for warnings related to encodings. |
| 825 | |
| 826 | See :ref:`io-encoding-warning` for details. |
| 827 | |
| 828 | .. versionadded:: 3.10 |
| 829 | |
| 830 | |
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 831 | .. exception:: BytesWarning |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 832 | |
| Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 833 | Base class for warnings related to :class:`bytes` and :class:`bytearray`. |
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 834 | |
| Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 835 | |
| 836 | .. exception:: ResourceWarning |
| 837 | |
| Victor Stinner | b9783d2 | 2020-01-24 10:22:18 +0100 | [diff] [blame] | 838 | Base class for warnings related to resource usage. |
| 839 | |
| 840 | Ignored by the default warning filters. Enabling the :ref:`Python |
| 841 | Development Mode <devmode>` shows this warning. |
| Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 842 | |
| 843 | .. versionadded:: 3.2 |
| 844 | |
| 845 | |
| 846 | |
| Alexandre Vassalotti | c22c6f2 | 2009-07-21 00:51:58 +0000 | [diff] [blame] | 847 | Exception hierarchy |
| 848 | ------------------- |
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 849 | |
| 850 | The class hierarchy for built-in exceptions is: |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 851 | |
| 852 | .. literalinclude:: ../../Lib/test/exception_hierarchy.txt |