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