[2] | 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"
|
---|
[391] | 29 | indicating the detailed cause of the error. This may be a string or a tuple
|
---|
[2] | 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 |
|
---|
| 49 | .. exception:: BaseException
|
---|
| 50 |
|
---|
| 51 | The base class for all built-in exceptions. It is not meant to be directly
|
---|
[391] | 52 | inherited by user-defined classes (for that, use :exc:`Exception`). If
|
---|
[2] | 53 | :func:`str` or :func:`unicode` is called on an instance of this class, the
|
---|
[391] | 54 | representation of the argument(s) to the instance are returned, or the empty
|
---|
| 55 | string when there were no arguments.
|
---|
[2] | 56 |
|
---|
| 57 | .. versionadded:: 2.5
|
---|
| 58 |
|
---|
[391] | 59 | .. attribute:: args
|
---|
[2] | 60 |
|
---|
[391] | 61 | The tuple of arguments given to the exception constructor. Some built-in
|
---|
| 62 | exceptions (like :exc:`IOError`) expect a certain number of arguments and
|
---|
| 63 | assign a special meaning to the elements of this tuple, while others are
|
---|
| 64 | usually called only with a single string giving an error message.
|
---|
| 65 |
|
---|
| 66 |
|
---|
[2] | 67 | .. exception:: Exception
|
---|
| 68 |
|
---|
| 69 | All built-in, non-system-exiting exceptions are derived from this class. All
|
---|
| 70 | user-defined exceptions should also be derived from this class.
|
---|
| 71 |
|
---|
| 72 | .. versionchanged:: 2.5
|
---|
| 73 | Changed to inherit from :exc:`BaseException`.
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | .. exception:: StandardError
|
---|
| 77 |
|
---|
| 78 | The base class for all built-in exceptions except :exc:`StopIteration`,
|
---|
| 79 | :exc:`GeneratorExit`, :exc:`KeyboardInterrupt` and :exc:`SystemExit`.
|
---|
| 80 | :exc:`StandardError` itself is derived from :exc:`Exception`.
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | .. exception:: ArithmeticError
|
---|
| 84 |
|
---|
| 85 | The base class for those built-in exceptions that are raised for various
|
---|
| 86 | arithmetic errors: :exc:`OverflowError`, :exc:`ZeroDivisionError`,
|
---|
| 87 | :exc:`FloatingPointError`.
|
---|
| 88 |
|
---|
| 89 |
|
---|
[391] | 90 | .. exception:: BufferError
|
---|
| 91 |
|
---|
| 92 | Raised when a :ref:`buffer <bufferobjects>` related operation cannot be
|
---|
| 93 | performed.
|
---|
| 94 |
|
---|
| 95 |
|
---|
[2] | 96 | .. exception:: LookupError
|
---|
| 97 |
|
---|
| 98 | The base class for the exceptions that are raised when a key or index used on
|
---|
| 99 | a mapping or sequence is invalid: :exc:`IndexError`, :exc:`KeyError`. This
|
---|
| 100 | can be raised directly by :func:`codecs.lookup`.
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 | .. exception:: EnvironmentError
|
---|
| 104 |
|
---|
| 105 | The base class for exceptions that can occur outside the Python system:
|
---|
| 106 | :exc:`IOError`, :exc:`OSError`. When exceptions of this type are created with a
|
---|
| 107 | 2-tuple, the first item is available on the instance's :attr:`errno` attribute
|
---|
| 108 | (it is assumed to be an error number), and the second item is available on the
|
---|
| 109 | :attr:`strerror` attribute (it is usually the associated error message). The
|
---|
| 110 | tuple itself is also available on the :attr:`args` attribute.
|
---|
| 111 |
|
---|
| 112 | .. versionadded:: 1.5.2
|
---|
| 113 |
|
---|
| 114 | When an :exc:`EnvironmentError` exception is instantiated with a 3-tuple, the
|
---|
| 115 | first two items are available as above, while the third item is available on the
|
---|
| 116 | :attr:`filename` attribute. However, for backwards compatibility, the
|
---|
| 117 | :attr:`args` attribute contains only a 2-tuple of the first two constructor
|
---|
| 118 | arguments.
|
---|
| 119 |
|
---|
| 120 | The :attr:`filename` attribute is ``None`` when this exception is created with
|
---|
| 121 | other than 3 arguments. The :attr:`errno` and :attr:`strerror` attributes are
|
---|
| 122 | also ``None`` when the instance was created with other than 2 or 3 arguments.
|
---|
| 123 | In this last case, :attr:`args` contains the verbatim constructor arguments as a
|
---|
| 124 | tuple.
|
---|
| 125 |
|
---|
| 126 | The following exceptions are the exceptions that are actually raised.
|
---|
| 127 |
|
---|
| 128 |
|
---|
| 129 | .. exception:: AssertionError
|
---|
| 130 |
|
---|
| 131 | .. index:: statement: assert
|
---|
| 132 |
|
---|
| 133 | Raised when an :keyword:`assert` statement fails.
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 | .. exception:: AttributeError
|
---|
| 137 |
|
---|
| 138 | Raised when an attribute reference (see :ref:`attribute-references`) or
|
---|
| 139 | assignment fails. (When an object does not support attribute references or
|
---|
| 140 | attribute assignments at all, :exc:`TypeError` is raised.)
|
---|
| 141 |
|
---|
| 142 |
|
---|
| 143 | .. exception:: EOFError
|
---|
| 144 |
|
---|
| 145 | Raised when one of the built-in functions (:func:`input` or :func:`raw_input`)
|
---|
| 146 | hits an end-of-file condition (EOF) without reading any data. (N.B.: the
|
---|
| 147 | :meth:`file.read` and :meth:`file.readline` methods return an empty string
|
---|
| 148 | when they hit EOF.)
|
---|
| 149 |
|
---|
| 150 |
|
---|
| 151 | .. exception:: FloatingPointError
|
---|
| 152 |
|
---|
| 153 | Raised when a floating point operation fails. This exception is always defined,
|
---|
| 154 | but can only be raised when Python is configured with the
|
---|
[391] | 155 | ``--with-fpectl`` option, or the :const:`WANT_SIGFPE_HANDLER` symbol is
|
---|
[2] | 156 | defined in the :file:`pyconfig.h` file.
|
---|
| 157 |
|
---|
| 158 |
|
---|
| 159 | .. exception:: GeneratorExit
|
---|
| 160 |
|
---|
| 161 | Raise when a :term:`generator`\'s :meth:`close` method is called. It
|
---|
| 162 | directly inherits from :exc:`BaseException` instead of :exc:`StandardError` since
|
---|
| 163 | it is technically not an error.
|
---|
| 164 |
|
---|
| 165 | .. versionadded:: 2.5
|
---|
| 166 |
|
---|
| 167 | .. versionchanged:: 2.6
|
---|
| 168 | Changed to inherit from :exc:`BaseException`.
|
---|
| 169 |
|
---|
| 170 | .. exception:: IOError
|
---|
| 171 |
|
---|
| 172 | Raised when an I/O operation (such as a :keyword:`print` statement, the built-in
|
---|
| 173 | :func:`open` function or a method of a file object) fails for an I/O-related
|
---|
| 174 | reason, e.g., "file not found" or "disk full".
|
---|
| 175 |
|
---|
| 176 | This class is derived from :exc:`EnvironmentError`. See the discussion above
|
---|
| 177 | for more information on exception instance attributes.
|
---|
| 178 |
|
---|
| 179 | .. versionchanged:: 2.6
|
---|
| 180 | Changed :exc:`socket.error` to use this as a base class.
|
---|
| 181 |
|
---|
| 182 |
|
---|
| 183 | .. exception:: ImportError
|
---|
| 184 |
|
---|
| 185 | Raised when an :keyword:`import` statement fails to find the module definition
|
---|
| 186 | or when a ``from ... import`` fails to find a name that is to be imported.
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | .. exception:: IndexError
|
---|
| 190 |
|
---|
| 191 | Raised when a sequence subscript is out of range. (Slice indices are silently
|
---|
| 192 | truncated to fall in the allowed range; if an index is not a plain integer,
|
---|
| 193 | :exc:`TypeError` is raised.)
|
---|
| 194 |
|
---|
| 195 | .. XXX xref to sequences
|
---|
| 196 |
|
---|
| 197 |
|
---|
| 198 | .. exception:: KeyError
|
---|
| 199 |
|
---|
| 200 | Raised when a mapping (dictionary) key is not found in the set of existing keys.
|
---|
| 201 |
|
---|
| 202 | .. XXX xref to mapping objects?
|
---|
| 203 |
|
---|
| 204 |
|
---|
| 205 | .. exception:: KeyboardInterrupt
|
---|
| 206 |
|
---|
| 207 | Raised when the user hits the interrupt key (normally :kbd:`Control-C` or
|
---|
| 208 | :kbd:`Delete`). During execution, a check for interrupts is made regularly.
|
---|
| 209 | Interrupts typed when a built-in function :func:`input` or :func:`raw_input` is
|
---|
| 210 | waiting for input also raise this exception. The exception inherits from
|
---|
| 211 | :exc:`BaseException` so as to not be accidentally caught by code that catches
|
---|
| 212 | :exc:`Exception` and thus prevent the interpreter from exiting.
|
---|
| 213 |
|
---|
| 214 | .. versionchanged:: 2.5
|
---|
| 215 | Changed to inherit from :exc:`BaseException`.
|
---|
| 216 |
|
---|
| 217 |
|
---|
| 218 | .. exception:: MemoryError
|
---|
| 219 |
|
---|
| 220 | Raised when an operation runs out of memory but the situation may still be
|
---|
| 221 | rescued (by deleting some objects). The associated value is a string indicating
|
---|
| 222 | what kind of (internal) operation ran out of memory. Note that because of the
|
---|
[391] | 223 | underlying memory management architecture (C's :c:func:`malloc` function), the
|
---|
[2] | 224 | interpreter may not always be able to completely recover from this situation; it
|
---|
| 225 | nevertheless raises an exception so that a stack traceback can be printed, in
|
---|
| 226 | case a run-away program was the cause.
|
---|
| 227 |
|
---|
| 228 |
|
---|
| 229 | .. exception:: NameError
|
---|
| 230 |
|
---|
| 231 | Raised when a local or global name is not found. This applies only to
|
---|
| 232 | unqualified names. The associated value is an error message that includes the
|
---|
| 233 | name that could not be found.
|
---|
| 234 |
|
---|
| 235 |
|
---|
| 236 | .. exception:: NotImplementedError
|
---|
| 237 |
|
---|
| 238 | This exception is derived from :exc:`RuntimeError`. In user defined base
|
---|
| 239 | classes, abstract methods should raise this exception when they require derived
|
---|
| 240 | classes to override the method.
|
---|
| 241 |
|
---|
| 242 | .. versionadded:: 1.5.2
|
---|
| 243 |
|
---|
| 244 |
|
---|
| 245 | .. exception:: OSError
|
---|
| 246 |
|
---|
| 247 | .. index:: module: errno
|
---|
| 248 |
|
---|
| 249 | This exception is derived from :exc:`EnvironmentError`. It is raised when a
|
---|
| 250 | function returns a system-related error (not for illegal argument types or
|
---|
| 251 | other incidental errors). The :attr:`errno` attribute is a numeric error
|
---|
[391] | 252 | code from :c:data:`errno`, and the :attr:`strerror` attribute is the
|
---|
| 253 | corresponding string, as would be printed by the C function :c:func:`perror`.
|
---|
[2] | 254 | See the module :mod:`errno`, which contains names for the error codes defined
|
---|
| 255 | by the underlying operating system.
|
---|
| 256 |
|
---|
| 257 | For exceptions that involve a file system path (such as :func:`chdir` or
|
---|
| 258 | :func:`unlink`), the exception instance will contain a third attribute,
|
---|
| 259 | :attr:`filename`, which is the file name passed to the function.
|
---|
| 260 |
|
---|
| 261 | .. versionadded:: 1.5.2
|
---|
| 262 |
|
---|
| 263 |
|
---|
| 264 | .. exception:: OverflowError
|
---|
| 265 |
|
---|
| 266 | Raised when the result of an arithmetic operation is too large to be
|
---|
| 267 | represented. This cannot occur for long integers (which would rather raise
|
---|
| 268 | :exc:`MemoryError` than give up) and for most operations with plain integers,
|
---|
| 269 | which return a long integer instead. Because of the lack of standardization
|
---|
| 270 | of floating point exception handling in C, most floating point operations
|
---|
| 271 | also aren't checked.
|
---|
| 272 |
|
---|
| 273 |
|
---|
| 274 | .. exception:: ReferenceError
|
---|
| 275 |
|
---|
| 276 | This exception is raised when a weak reference proxy, created by the
|
---|
| 277 | :func:`weakref.proxy` function, is used to access an attribute of the referent
|
---|
| 278 | after it has been garbage collected. For more information on weak references,
|
---|
| 279 | see the :mod:`weakref` module.
|
---|
| 280 |
|
---|
| 281 | .. versionadded:: 2.2
|
---|
| 282 | Previously known as the :exc:`weakref.ReferenceError` exception.
|
---|
| 283 |
|
---|
| 284 |
|
---|
| 285 | .. exception:: RuntimeError
|
---|
| 286 |
|
---|
| 287 | Raised when an error is detected that doesn't fall in any of the other
|
---|
| 288 | categories. The associated value is a string indicating what precisely went
|
---|
| 289 | wrong. (This exception is mostly a relic from a previous version of the
|
---|
| 290 | interpreter; it is not used very much any more.)
|
---|
| 291 |
|
---|
| 292 |
|
---|
| 293 | .. exception:: StopIteration
|
---|
| 294 |
|
---|
| 295 | Raised by an :term:`iterator`\'s :meth:`~iterator.next` method to signal that
|
---|
| 296 | there are no further values. This is derived from :exc:`Exception` rather
|
---|
| 297 | than :exc:`StandardError`, since this is not considered an error in its
|
---|
| 298 | normal application.
|
---|
| 299 |
|
---|
| 300 | .. versionadded:: 2.2
|
---|
| 301 |
|
---|
| 302 |
|
---|
| 303 | .. exception:: SyntaxError
|
---|
| 304 |
|
---|
| 305 | Raised when the parser encounters a syntax error. This may occur in an
|
---|
| 306 | :keyword:`import` statement, in an :keyword:`exec` statement, in a call to the
|
---|
| 307 | built-in function :func:`eval` or :func:`input`, or when reading the initial
|
---|
| 308 | script or standard input (also interactively).
|
---|
| 309 |
|
---|
| 310 | Instances of this class have attributes :attr:`filename`, :attr:`lineno`,
|
---|
| 311 | :attr:`offset` and :attr:`text` for easier access to the details. :func:`str`
|
---|
| 312 | of the exception instance returns only the message.
|
---|
| 313 |
|
---|
| 314 |
|
---|
[391] | 315 | .. exception:: IndentationError
|
---|
| 316 |
|
---|
| 317 | Base class for syntax errors related to incorrect indentation. This is a
|
---|
| 318 | subclass of :exc:`SyntaxError`.
|
---|
| 319 |
|
---|
| 320 |
|
---|
| 321 | .. exception:: TabError
|
---|
| 322 |
|
---|
| 323 | Raised when indentation contains an inconsistent use of tabs and spaces.
|
---|
| 324 | This is a subclass of :exc:`IndentationError`.
|
---|
| 325 |
|
---|
| 326 |
|
---|
[2] | 327 | .. exception:: SystemError
|
---|
| 328 |
|
---|
| 329 | Raised when the interpreter finds an internal error, but the situation does not
|
---|
| 330 | look so serious to cause it to abandon all hope. The associated value is a
|
---|
| 331 | string indicating what went wrong (in low-level terms).
|
---|
| 332 |
|
---|
| 333 | You should report this to the author or maintainer of your Python interpreter.
|
---|
| 334 | Be sure to report the version of the Python interpreter (``sys.version``; it is
|
---|
| 335 | also printed at the start of an interactive Python session), the exact error
|
---|
| 336 | message (the exception's associated value) and if possible the source of the
|
---|
| 337 | program that triggered the error.
|
---|
| 338 |
|
---|
| 339 |
|
---|
| 340 | .. exception:: SystemExit
|
---|
| 341 |
|
---|
| 342 | This exception is raised by the :func:`sys.exit` function. When it is not
|
---|
| 343 | handled, the Python interpreter exits; no stack traceback is printed. If the
|
---|
| 344 | associated value is a plain integer, it specifies the system exit status (passed
|
---|
[391] | 345 | to C's :c:func:`exit` function); if it is ``None``, the exit status is zero; if
|
---|
[2] | 346 | it has another type (such as a string), the object's value is printed and the
|
---|
| 347 | exit status is one.
|
---|
| 348 |
|
---|
[391] | 349 | Instances have an attribute :attr:`!code` which is set to the proposed exit
|
---|
[2] | 350 | status or error message (defaulting to ``None``). Also, this exception derives
|
---|
| 351 | directly from :exc:`BaseException` and not :exc:`StandardError`, since it is not
|
---|
| 352 | technically an error.
|
---|
| 353 |
|
---|
| 354 | A call to :func:`sys.exit` is translated into an exception so that clean-up
|
---|
| 355 | handlers (:keyword:`finally` clauses of :keyword:`try` statements) can be
|
---|
| 356 | executed, and so that a debugger can execute a script without running the risk
|
---|
| 357 | of losing control. The :func:`os._exit` function can be used if it is
|
---|
| 358 | absolutely positively necessary to exit immediately (for example, in the child
|
---|
[391] | 359 | process after a call to :func:`os.fork`).
|
---|
[2] | 360 |
|
---|
| 361 | The exception inherits from :exc:`BaseException` instead of :exc:`StandardError`
|
---|
| 362 | or :exc:`Exception` so that it is not accidentally caught by code that catches
|
---|
| 363 | :exc:`Exception`. This allows the exception to properly propagate up and cause
|
---|
| 364 | the interpreter to exit.
|
---|
| 365 |
|
---|
| 366 | .. versionchanged:: 2.5
|
---|
| 367 | Changed to inherit from :exc:`BaseException`.
|
---|
| 368 |
|
---|
| 369 |
|
---|
| 370 | .. exception:: TypeError
|
---|
| 371 |
|
---|
| 372 | Raised when an operation or function is applied to an object of inappropriate
|
---|
| 373 | type. The associated value is a string giving details about the type mismatch.
|
---|
| 374 |
|
---|
| 375 |
|
---|
| 376 | .. exception:: UnboundLocalError
|
---|
| 377 |
|
---|
| 378 | Raised when a reference is made to a local variable in a function or method, but
|
---|
| 379 | no value has been bound to that variable. This is a subclass of
|
---|
| 380 | :exc:`NameError`.
|
---|
| 381 |
|
---|
| 382 | .. versionadded:: 2.0
|
---|
| 383 |
|
---|
| 384 |
|
---|
| 385 | .. exception:: UnicodeError
|
---|
| 386 |
|
---|
| 387 | Raised when a Unicode-related encoding or decoding error occurs. It is a
|
---|
| 388 | subclass of :exc:`ValueError`.
|
---|
| 389 |
|
---|
[391] | 390 | :exc:`UnicodeError` has attributes that describe the encoding or decoding
|
---|
| 391 | error. For example, ``err.object[err.start:err.end]`` gives the particular
|
---|
| 392 | invalid input that the codec failed on.
|
---|
| 393 |
|
---|
| 394 | .. attribute:: encoding
|
---|
| 395 |
|
---|
| 396 | The name of the encoding that raised the error.
|
---|
| 397 |
|
---|
| 398 | .. attribute:: reason
|
---|
| 399 |
|
---|
| 400 | A string describing the specific codec error.
|
---|
| 401 |
|
---|
| 402 | .. attribute:: object
|
---|
| 403 |
|
---|
| 404 | The object the codec was attempting to encode or decode.
|
---|
| 405 |
|
---|
| 406 | .. attribute:: start
|
---|
| 407 |
|
---|
| 408 | The first index of invalid data in :attr:`object`.
|
---|
| 409 |
|
---|
| 410 | .. attribute:: end
|
---|
| 411 |
|
---|
| 412 | The index after the last invalid data in :attr:`object`.
|
---|
| 413 |
|
---|
[2] | 414 | .. versionadded:: 2.0
|
---|
| 415 |
|
---|
| 416 |
|
---|
| 417 | .. exception:: UnicodeEncodeError
|
---|
| 418 |
|
---|
| 419 | Raised when a Unicode-related error occurs during encoding. It is a subclass of
|
---|
| 420 | :exc:`UnicodeError`.
|
---|
| 421 |
|
---|
| 422 | .. versionadded:: 2.3
|
---|
| 423 |
|
---|
| 424 |
|
---|
| 425 | .. exception:: UnicodeDecodeError
|
---|
| 426 |
|
---|
| 427 | Raised when a Unicode-related error occurs during decoding. It is a subclass of
|
---|
| 428 | :exc:`UnicodeError`.
|
---|
| 429 |
|
---|
| 430 | .. versionadded:: 2.3
|
---|
| 431 |
|
---|
| 432 |
|
---|
| 433 | .. exception:: UnicodeTranslateError
|
---|
| 434 |
|
---|
| 435 | Raised when a Unicode-related error occurs during translating. It is a subclass
|
---|
| 436 | of :exc:`UnicodeError`.
|
---|
| 437 |
|
---|
| 438 | .. versionadded:: 2.3
|
---|
| 439 |
|
---|
| 440 |
|
---|
| 441 | .. exception:: ValueError
|
---|
| 442 |
|
---|
| 443 | Raised when a built-in operation or function receives an argument that has the
|
---|
| 444 | right type but an inappropriate value, and the situation is not described by a
|
---|
| 445 | more precise exception such as :exc:`IndexError`.
|
---|
| 446 |
|
---|
| 447 |
|
---|
| 448 | .. exception:: VMSError
|
---|
| 449 |
|
---|
| 450 | Only available on VMS. Raised when a VMS-specific error occurs.
|
---|
| 451 |
|
---|
| 452 |
|
---|
| 453 | .. exception:: WindowsError
|
---|
| 454 |
|
---|
| 455 | Raised when a Windows-specific error occurs or when the error number does not
|
---|
[391] | 456 | correspond to an :c:data:`errno` value. The :attr:`winerror` and
|
---|
[2] | 457 | :attr:`strerror` values are created from the return values of the
|
---|
[391] | 458 | :c:func:`GetLastError` and :c:func:`FormatMessage` functions from the Windows
|
---|
[2] | 459 | Platform API. The :attr:`errno` value maps the :attr:`winerror` value to
|
---|
| 460 | corresponding ``errno.h`` values. This is a subclass of :exc:`OSError`.
|
---|
| 461 |
|
---|
| 462 | .. versionadded:: 2.0
|
---|
| 463 |
|
---|
| 464 | .. versionchanged:: 2.5
|
---|
[391] | 465 | Previous versions put the :c:func:`GetLastError` codes into :attr:`errno`.
|
---|
[2] | 466 |
|
---|
| 467 |
|
---|
| 468 | .. exception:: ZeroDivisionError
|
---|
| 469 |
|
---|
| 470 | Raised when the second argument of a division or modulo operation is zero. The
|
---|
| 471 | associated value is a string indicating the type of the operands and the
|
---|
| 472 | operation.
|
---|
| 473 |
|
---|
| 474 | The following exceptions are used as warning categories; see the :mod:`warnings`
|
---|
| 475 | module for more information.
|
---|
| 476 |
|
---|
| 477 |
|
---|
| 478 | .. exception:: Warning
|
---|
| 479 |
|
---|
| 480 | Base class for warning categories.
|
---|
| 481 |
|
---|
| 482 |
|
---|
| 483 | .. exception:: UserWarning
|
---|
| 484 |
|
---|
| 485 | Base class for warnings generated by user code.
|
---|
| 486 |
|
---|
| 487 |
|
---|
| 488 | .. exception:: DeprecationWarning
|
---|
| 489 |
|
---|
| 490 | Base class for warnings about deprecated features.
|
---|
| 491 |
|
---|
| 492 |
|
---|
| 493 | .. exception:: PendingDeprecationWarning
|
---|
| 494 |
|
---|
| 495 | Base class for warnings about features which will be deprecated in the future.
|
---|
| 496 |
|
---|
| 497 |
|
---|
| 498 | .. exception:: SyntaxWarning
|
---|
| 499 |
|
---|
| 500 | Base class for warnings about dubious syntax
|
---|
| 501 |
|
---|
| 502 |
|
---|
| 503 | .. exception:: RuntimeWarning
|
---|
| 504 |
|
---|
| 505 | Base class for warnings about dubious runtime behavior.
|
---|
| 506 |
|
---|
| 507 |
|
---|
| 508 | .. exception:: FutureWarning
|
---|
| 509 |
|
---|
| 510 | Base class for warnings about constructs that will change semantically in the
|
---|
| 511 | future.
|
---|
| 512 |
|
---|
| 513 |
|
---|
| 514 | .. exception:: ImportWarning
|
---|
| 515 |
|
---|
| 516 | Base class for warnings about probable mistakes in module imports.
|
---|
| 517 |
|
---|
| 518 | .. versionadded:: 2.5
|
---|
| 519 |
|
---|
| 520 |
|
---|
| 521 | .. exception:: UnicodeWarning
|
---|
| 522 |
|
---|
| 523 | Base class for warnings related to Unicode.
|
---|
| 524 |
|
---|
| 525 | .. versionadded:: 2.5
|
---|
| 526 |
|
---|
| 527 |
|
---|
| 528 | Exception hierarchy
|
---|
| 529 | -------------------
|
---|
| 530 |
|
---|
| 531 | The class hierarchy for built-in exceptions is:
|
---|
| 532 |
|
---|
| 533 | .. literalinclude:: ../../Lib/test/exception_hierarchy.txt
|
---|