Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | .. _bltin-exceptions: |
| 2 | |
| 3 | Built-in Exceptions |
| 4 | =================== |
| 5 | |
| 6 | .. module:: exceptions |
| 7 | :synopsis: Standard exception classes. |
| 8 | |
| 9 | |
| 10 | Exceptions should be class objects. The exceptions are defined in the module |
| 11 | :mod:`exceptions`. This module never needs to be imported explicitly: the |
| 12 | exceptions are provided in the built-in namespace as well as the |
| 13 | :mod:`exceptions` module. |
| 14 | |
| 15 | .. index:: |
| 16 | statement: try |
| 17 | statement: except |
| 18 | |
| 19 | For class exceptions, in a :keyword:`try` statement with an :keyword:`except` |
| 20 | clause that mentions a particular class, that clause also handles any exception |
| 21 | classes derived from that class (but not exception classes from which *it* is |
| 22 | derived). Two exception classes that are not related via subclassing are never |
| 23 | equivalent, even if they have the same name. |
| 24 | |
| 25 | .. index:: statement: raise |
| 26 | |
| 27 | The built-in exceptions listed below can be generated by the interpreter or |
| 28 | built-in functions. Except where mentioned, they have an "associated value" |
| 29 | indicating the detailed cause of the error. This may be a string or a tuple |
| 30 | containing several items of information (e.g., an error code and a string |
| 31 | explaining the code). The associated value is the second argument to the |
| 32 | :keyword:`raise` statement. If the exception class is derived from the standard |
| 33 | root class :exc:`BaseException`, the associated value is present as the |
| 34 | exception instance's :attr:`args` attribute. |
| 35 | |
| 36 | User code can raise built-in exceptions. This can be used to test an exception |
| 37 | handler or to report an error condition "just like" the situation in which the |
| 38 | interpreter raises the same exception; but beware that there is nothing to |
| 39 | prevent user code from raising an inappropriate error. |
| 40 | |
| 41 | The built-in exception classes can be sub-classed to define new exceptions; |
| 42 | programmers are encouraged to at least derive new exceptions from the |
| 43 | :exc:`Exception` class and not :exc:`BaseException`. More information on |
| 44 | defining exceptions is available in the Python Tutorial under |
| 45 | :ref:`tut-userexceptions`. |
| 46 | |
| 47 | The following exceptions are only used as base classes for other exceptions. |
| 48 | |
Georg Brandl | 02c3056 | 2007-09-07 17:52:53 +0000 | [diff] [blame] | 49 | .. XXX document with_traceback() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 50 | |
| 51 | .. exception:: BaseException |
| 52 | |
| 53 | The base class for all built-in exceptions. It is not meant to be directly |
| 54 | inherited by user-defined classes (for that use :exc:`Exception`). If |
| 55 | :func:`str` or :func:`unicode` is called on an instance of this class, the |
| 56 | representation of the argument(s) to the instance are returned or the emptry |
| 57 | string when there were no arguments. All arguments are stored in :attr:`args` |
| 58 | as a tuple. |
| 59 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | |
| 61 | .. exception:: Exception |
| 62 | |
| 63 | All built-in, non-system-exiting exceptions are derived from this class. All |
| 64 | user-defined exceptions should also be derived from this class. |
| 65 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | |
| 67 | .. exception:: ArithmeticError |
| 68 | |
| 69 | The base class for those built-in exceptions that are raised for various |
| 70 | arithmetic errors: :exc:`OverflowError`, :exc:`ZeroDivisionError`, |
| 71 | :exc:`FloatingPointError`. |
| 72 | |
| 73 | |
| 74 | .. exception:: LookupError |
| 75 | |
| 76 | The base class for the exceptions that are raised when a key or index used on a |
| 77 | mapping or sequence is invalid: :exc:`IndexError`, :exc:`KeyError`. This can be |
| 78 | raised directly by :func:`sys.setdefaultencoding`. |
| 79 | |
| 80 | |
| 81 | .. exception:: EnvironmentError |
| 82 | |
| 83 | The base class for exceptions that can occur outside the Python system: |
| 84 | :exc:`IOError`, :exc:`OSError`. When exceptions of this type are created with a |
| 85 | 2-tuple, the first item is available on the instance's :attr:`errno` attribute |
| 86 | (it is assumed to be an error number), and the second item is available on the |
| 87 | :attr:`strerror` attribute (it is usually the associated error message). The |
| 88 | tuple itself is also available on the :attr:`args` attribute. |
| 89 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 | When an :exc:`EnvironmentError` exception is instantiated with a 3-tuple, the |
| 91 | first two items are available as above, while the third item is available on the |
| 92 | :attr:`filename` attribute. However, for backwards compatibility, the |
| 93 | :attr:`args` attribute contains only a 2-tuple of the first two constructor |
| 94 | arguments. |
| 95 | |
| 96 | The :attr:`filename` attribute is ``None`` when this exception is created with |
| 97 | other than 3 arguments. The :attr:`errno` and :attr:`strerror` attributes are |
| 98 | also ``None`` when the instance was created with other than 2 or 3 arguments. |
| 99 | In this last case, :attr:`args` contains the verbatim constructor arguments as a |
| 100 | tuple. |
| 101 | |
| 102 | The following exceptions are the exceptions that are actually raised. |
| 103 | |
| 104 | |
| 105 | .. exception:: AssertionError |
| 106 | |
| 107 | .. index:: statement: assert |
| 108 | |
| 109 | Raised when an :keyword:`assert` statement fails. |
| 110 | |
| 111 | |
| 112 | .. exception:: AttributeError |
| 113 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 114 | Raised when an attribute reference (see :ref:`attribute-references`) or |
| 115 | assignment fails. (When an object does not support attribute references or |
| 116 | attribute assignments at all, :exc:`TypeError` is raised.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 117 | |
| 118 | |
| 119 | .. exception:: EOFError |
| 120 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 121 | Raised when one of the built-in functions (:func:`input` or :func:`raw_input`) |
| 122 | hits an end-of-file condition (EOF) without reading any data. (N.B.: the |
Georg Brandl | 81ac1ce | 2007-08-31 17:17:17 +0000 | [diff] [blame] | 123 | :meth:`file.read` and :meth:`file.readline` methods return an empty string |
| 124 | when they hit EOF.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 125 | |
| 126 | |
| 127 | .. exception:: FloatingPointError |
| 128 | |
| 129 | Raised when a floating point operation fails. This exception is always defined, |
| 130 | but can only be raised when Python is configured with the |
| 131 | :option:`--with-fpectl` option, or the :const:`WANT_SIGFPE_HANDLER` symbol is |
| 132 | defined in the :file:`pyconfig.h` file. |
| 133 | |
| 134 | |
| 135 | .. exception:: GeneratorExit |
| 136 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 137 | Raise when a :term:`generator`\'s :meth:`close` method is called. It |
| 138 | directly inherits from :exc:`BaseException` instead of :exc:`Exception` since |
| 139 | it is technically not an error. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 140 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 141 | |
| 142 | .. exception:: IOError |
| 143 | |
Georg Brandl | 81ac1ce | 2007-08-31 17:17:17 +0000 | [diff] [blame] | 144 | Raised when an I/O operation (such as the built-in :func:`print` or |
| 145 | :func:`open` functions or a method of a file object) fails for an I/O-related |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 146 | reason, e.g., "file not found" or "disk full". |
| 147 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 148 | This class is derived from :exc:`EnvironmentError`. See the discussion above |
| 149 | for more information on exception instance attributes. |
| 150 | |
| 151 | |
| 152 | .. exception:: ImportError |
| 153 | |
| 154 | Raised when an :keyword:`import` statement fails to find the module definition |
| 155 | or when a ``from ... import`` fails to find a name that is to be imported. |
| 156 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 157 | |
| 158 | .. exception:: IndexError |
| 159 | |
Georg Brandl | 95817b3 | 2008-05-11 14:30:18 +0000 | [diff] [blame] | 160 | Raised when a sequence subscript is out of range. (Slice indices are |
| 161 | silently truncated to fall in the allowed range; if an index is not an |
| 162 | integer, :exc:`TypeError` is raised.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 163 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 164 | .. XXX xref to sequences |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 165 | |
| 166 | |
| 167 | .. exception:: KeyError |
| 168 | |
| 169 | Raised when a mapping (dictionary) key is not found in the set of existing keys. |
| 170 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 171 | .. XXX xref to mapping objects? |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 172 | |
| 173 | |
| 174 | .. exception:: KeyboardInterrupt |
| 175 | |
| 176 | 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] | 177 | :kbd:`Delete`). During execution, a check for interrupts is made |
| 178 | regularly. The exception inherits from :exc:`BaseException` so as to not be |
| 179 | accidentally caught by code that catches :exc:`Exception` and thus prevent |
| 180 | the interpreter from exiting. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 181 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 182 | |
| 183 | .. exception:: MemoryError |
| 184 | |
| 185 | Raised when an operation runs out of memory but the situation may still be |
| 186 | rescued (by deleting some objects). The associated value is a string indicating |
| 187 | what kind of (internal) operation ran out of memory. Note that because of the |
| 188 | underlying memory management architecture (C's :cfunc:`malloc` function), the |
| 189 | interpreter may not always be able to completely recover from this situation; it |
| 190 | nevertheless raises an exception so that a stack traceback can be printed, in |
| 191 | case a run-away program was the cause. |
| 192 | |
| 193 | |
| 194 | .. exception:: NameError |
| 195 | |
| 196 | Raised when a local or global name is not found. This applies only to |
| 197 | unqualified names. The associated value is an error message that includes the |
| 198 | name that could not be found. |
| 199 | |
| 200 | |
| 201 | .. exception:: NotImplementedError |
| 202 | |
| 203 | This exception is derived from :exc:`RuntimeError`. In user defined base |
| 204 | classes, abstract methods should raise this exception when they require derived |
| 205 | classes to override the method. |
| 206 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 207 | |
| 208 | .. exception:: OSError |
| 209 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 210 | .. index:: module: errno |
| 211 | |
| 212 | This exception is derived from :exc:`EnvironmentError`. It is raised when a |
| 213 | function returns a system-related error (not for illegal argument types or |
| 214 | other incidental errors). The :attr:`errno` attribute is a numeric error |
| 215 | code from :cdata:`errno`, and the :attr:`strerror` attribute is the |
| 216 | corresponding string, as would be printed by the C function :cfunc:`perror`. |
| 217 | See the module :mod:`errno`, which contains names for the error codes defined |
| 218 | by the underlying operating system. |
| 219 | |
| 220 | For exceptions that involve a file system path (such as :func:`chdir` or |
| 221 | :func:`unlink`), the exception instance will contain a third attribute, |
| 222 | :attr:`filename`, which is the file name passed to the function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 223 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 224 | |
| 225 | .. exception:: OverflowError |
| 226 | |
| 227 | 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] | 228 | represented. This cannot occur for integers (which would rather raise |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 229 | :exc:`MemoryError` than give up). Because of the lack of standardization of |
| 230 | floating point exception handling in C, most floating point operations also |
Georg Brandl | 81ac1ce | 2007-08-31 17:17:17 +0000 | [diff] [blame] | 231 | aren't checked. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 232 | |
| 233 | |
| 234 | .. exception:: ReferenceError |
| 235 | |
| 236 | This exception is raised when a weak reference proxy, created by the |
| 237 | :func:`weakref.proxy` function, is used to access an attribute of the referent |
| 238 | after it has been garbage collected. For more information on weak references, |
| 239 | see the :mod:`weakref` module. |
| 240 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 241 | |
| 242 | .. exception:: RuntimeError |
| 243 | |
| 244 | Raised when an error is detected that doesn't fall in any of the other |
| 245 | categories. The associated value is a string indicating what precisely went |
| 246 | wrong. (This exception is mostly a relic from a previous version of the |
| 247 | interpreter; it is not used very much any more.) |
| 248 | |
| 249 | |
| 250 | .. exception:: StopIteration |
| 251 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 252 | Raised by builtin :func:`next` and an :term:`iterator`\'s :meth:`__next__` |
| 253 | method to signal that there are no further values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 254 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 255 | |
| 256 | .. exception:: SyntaxError |
| 257 | |
| 258 | Raised when the parser encounters a syntax error. This may occur in an |
| 259 | :keyword:`import` statement, in a call to the built-in functions :func:`exec` |
| 260 | or :func:`eval`, or when reading the initial script or standard input |
| 261 | (also interactively). |
| 262 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 263 | Instances of this class have attributes :attr:`filename`, :attr:`lineno`, |
| 264 | :attr:`offset` and :attr:`text` for easier access to the details. :func:`str` |
| 265 | of the exception instance returns only the message. |
| 266 | |
| 267 | |
| 268 | .. exception:: SystemError |
| 269 | |
| 270 | Raised when the interpreter finds an internal error, but the situation does not |
| 271 | look so serious to cause it to abandon all hope. The associated value is a |
| 272 | string indicating what went wrong (in low-level terms). |
| 273 | |
| 274 | You should report this to the author or maintainer of your Python interpreter. |
| 275 | Be sure to report the version of the Python interpreter (``sys.version``; it is |
| 276 | also printed at the start of an interactive Python session), the exact error |
| 277 | message (the exception's associated value) and if possible the source of the |
| 278 | program that triggered the error. |
| 279 | |
| 280 | |
| 281 | .. exception:: SystemExit |
| 282 | |
| 283 | This exception is raised by the :func:`sys.exit` function. When it is not |
| 284 | handled, the Python interpreter exits; no stack traceback is printed. If the |
Georg Brandl | 95817b3 | 2008-05-11 14:30:18 +0000 | [diff] [blame] | 285 | associated value is an integer, it specifies the system exit status (passed |
| 286 | to C's :cfunc:`exit` function); if it is ``None``, the exit status is zero; |
| 287 | if it has another type (such as a string), the object's value is printed and |
| 288 | the exit status is one. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 289 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 290 | Instances have an attribute :attr:`code` which is set to the proposed exit |
| 291 | status or error message (defaulting to ``None``). Also, this exception derives |
| 292 | directly from :exc:`BaseException` and not :exc:`Exception`, since it is not |
| 293 | technically an error. |
| 294 | |
| 295 | A call to :func:`sys.exit` is translated into an exception so that clean-up |
| 296 | handlers (:keyword:`finally` clauses of :keyword:`try` statements) can be |
| 297 | executed, and so that a debugger can execute a script without running the risk |
| 298 | of losing control. The :func:`os._exit` function can be used if it is |
| 299 | absolutely positively necessary to exit immediately (for example, in the child |
| 300 | process after a call to :func:`fork`). |
| 301 | |
| 302 | The exception inherits from :exc:`BaseException` instead of :exc:`Exception` so |
| 303 | that it is not accidentally caught by code that catches :exc:`Exception`. This |
| 304 | allows the exception to properly propagate up and cause the interpreter to exit. |
| 305 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 306 | |
| 307 | .. exception:: TypeError |
| 308 | |
| 309 | Raised when an operation or function is applied to an object of inappropriate |
| 310 | type. The associated value is a string giving details about the type mismatch. |
| 311 | |
| 312 | |
| 313 | .. exception:: UnboundLocalError |
| 314 | |
| 315 | Raised when a reference is made to a local variable in a function or method, but |
| 316 | no value has been bound to that variable. This is a subclass of |
| 317 | :exc:`NameError`. |
| 318 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 319 | |
| 320 | .. exception:: UnicodeError |
| 321 | |
| 322 | Raised when a Unicode-related encoding or decoding error occurs. It is a |
| 323 | subclass of :exc:`ValueError`. |
| 324 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 325 | |
| 326 | .. exception:: UnicodeEncodeError |
| 327 | |
| 328 | Raised when a Unicode-related error occurs during encoding. It is a subclass of |
| 329 | :exc:`UnicodeError`. |
| 330 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 331 | |
| 332 | .. exception:: UnicodeDecodeError |
| 333 | |
| 334 | Raised when a Unicode-related error occurs during decoding. It is a subclass of |
| 335 | :exc:`UnicodeError`. |
| 336 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 337 | |
| 338 | .. exception:: UnicodeTranslateError |
| 339 | |
| 340 | Raised when a Unicode-related error occurs during translating. It is a subclass |
| 341 | of :exc:`UnicodeError`. |
| 342 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 343 | |
| 344 | .. exception:: ValueError |
| 345 | |
| 346 | Raised when a built-in operation or function receives an argument that has the |
| 347 | right type but an inappropriate value, and the situation is not described by a |
| 348 | more precise exception such as :exc:`IndexError`. |
| 349 | |
| 350 | |
| 351 | .. exception:: WindowsError |
| 352 | |
| 353 | Raised when a Windows-specific error occurs or when the error number does not |
| 354 | correspond to an :cdata:`errno` value. The :attr:`winerror` and |
| 355 | :attr:`strerror` values are created from the return values of the |
| 356 | :cfunc:`GetLastError` and :cfunc:`FormatMessage` functions from the Windows |
| 357 | Platform API. The :attr:`errno` value maps the :attr:`winerror` value to |
| 358 | corresponding ``errno.h`` values. This is a subclass of :exc:`OSError`. |
| 359 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 360 | |
| 361 | .. exception:: ZeroDivisionError |
| 362 | |
| 363 | Raised when the second argument of a division or modulo operation is zero. The |
| 364 | associated value is a string indicating the type of the operands and the |
| 365 | operation. |
| 366 | |
| 367 | The following exceptions are used as warning categories; see the :mod:`warnings` |
| 368 | module for more information. |
| 369 | |
| 370 | |
| 371 | .. exception:: Warning |
| 372 | |
| 373 | Base class for warning categories. |
| 374 | |
| 375 | |
| 376 | .. exception:: UserWarning |
| 377 | |
| 378 | Base class for warnings generated by user code. |
| 379 | |
| 380 | |
| 381 | .. exception:: DeprecationWarning |
| 382 | |
| 383 | Base class for warnings about deprecated features. |
| 384 | |
| 385 | |
| 386 | .. exception:: PendingDeprecationWarning |
| 387 | |
| 388 | Base class for warnings about features which will be deprecated in the future. |
| 389 | |
| 390 | |
| 391 | .. exception:: SyntaxWarning |
| 392 | |
| 393 | Base class for warnings about dubious syntax |
| 394 | |
| 395 | |
| 396 | .. exception:: RuntimeWarning |
| 397 | |
| 398 | Base class for warnings about dubious runtime behavior. |
| 399 | |
| 400 | |
| 401 | .. exception:: FutureWarning |
| 402 | |
| 403 | Base class for warnings about constructs that will change semantically in the |
| 404 | future. |
| 405 | |
| 406 | |
| 407 | .. exception:: ImportWarning |
| 408 | |
| 409 | Base class for warnings about probable mistakes in module imports. |
| 410 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 411 | |
| 412 | .. exception:: UnicodeWarning |
| 413 | |
| 414 | Base class for warnings related to Unicode. |
| 415 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 416 | .. exception:: BytesWarning |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 417 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 418 | Base class for warnings related to :class:`bytes` and :class:`buffer`. |
| 419 | |
| 420 | |
| 421 | The class hierarchy for built-in exceptions is: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 422 | |
| 423 | .. literalinclude:: ../../Lib/test/exception_hierarchy.txt |