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 | |
| 31 | The built-in exception classes can be sub-classed to define new exceptions; |
| 32 | programmers are encouraged to at least derive new exceptions from the |
| 33 | :exc:`Exception` class and not :exc:`BaseException`. More information on |
| 34 | defining exceptions is available in the Python Tutorial under |
| 35 | :ref:`tut-userexceptions`. |
| 36 | |
Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 37 | |
| 38 | Base classes |
| 39 | ------------ |
| 40 | |
Georg Brandl | fbd1b22 | 2009-12-29 21:38:35 +0000 | [diff] [blame] | 41 | The following exceptions are used mostly as base classes for other exceptions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 42 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 43 | .. exception:: BaseException |
| 44 | |
| 45 | 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] | 46 | 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] | 47 | :func:`str` is called on an instance of this class, the representation of |
| 48 | the argument(s) to the instance are returned, or the empty string when |
| 49 | there were no arguments. |
Georg Brandl | fb6fd5d | 2011-01-07 18:28:45 +0000 | [diff] [blame] | 50 | |
| 51 | .. attribute:: args |
| 52 | |
| 53 | The tuple of arguments given to the exception constructor. Some built-in |
| 54 | exceptions (like :exc:`IOError`) expect a certain number of arguments and |
| 55 | assign a special meaning to the elements of this tuple, while others are |
| 56 | usually called only with a single string giving an error message. |
| 57 | |
| 58 | .. method:: with_traceback(tb) |
| 59 | |
| 60 | This method sets *tb* as the new traceback for the exception and returns |
| 61 | the exception object. It is usually used in exception handling code like |
| 62 | this:: |
| 63 | |
| 64 | try: |
| 65 | ... |
| 66 | except SomeException: |
| 67 | tb = sys.exc_info()[2] |
| 68 | raise OtherException(...).with_traceback(tb) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 70 | |
| 71 | .. exception:: Exception |
| 72 | |
| 73 | All built-in, non-system-exiting exceptions are derived from this class. All |
| 74 | user-defined exceptions should also be derived from this class. |
| 75 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | |
| 77 | .. exception:: ArithmeticError |
| 78 | |
| 79 | The base class for those built-in exceptions that are raised for various |
| 80 | arithmetic errors: :exc:`OverflowError`, :exc:`ZeroDivisionError`, |
| 81 | :exc:`FloatingPointError`. |
| 82 | |
| 83 | |
Georg Brandl | 0bdfbfa | 2010-12-18 17:51:28 +0000 | [diff] [blame] | 84 | .. exception:: BufferError |
| 85 | |
| 86 | Raised when a :ref:`buffer <bufferobjects>` related operation cannot be |
| 87 | performed. |
| 88 | |
| 89 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 | .. exception:: LookupError |
| 91 | |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 92 | The base class for the exceptions that are raised when a key or index used on |
| 93 | a mapping or sequence is invalid: :exc:`IndexError`, :exc:`KeyError`. This |
| 94 | can be raised directly by :func:`codecs.lookup`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 95 | |
| 96 | |
Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 97 | Concrete exceptions |
| 98 | ------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 99 | |
Georg Brandl | fbd1b22 | 2009-12-29 21:38:35 +0000 | [diff] [blame] | 100 | The following exceptions are the exceptions that are usually raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 101 | |
| 102 | .. exception:: AssertionError |
| 103 | |
| 104 | .. index:: statement: assert |
| 105 | |
| 106 | Raised when an :keyword:`assert` statement fails. |
| 107 | |
| 108 | |
| 109 | .. exception:: AttributeError |
| 110 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 111 | Raised when an attribute reference (see :ref:`attribute-references`) or |
| 112 | assignment fails. (When an object does not support attribute references or |
| 113 | attribute assignments at all, :exc:`TypeError` is raised.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 114 | |
| 115 | |
| 116 | .. exception:: EOFError |
| 117 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 118 | Raised when one of the built-in functions (:func:`input` or :func:`raw_input`) |
| 119 | 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] | 120 | :meth:`file.read` and :meth:`file.readline` methods return an empty string |
| 121 | when they hit EOF.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 122 | |
| 123 | |
| 124 | .. exception:: FloatingPointError |
| 125 | |
| 126 | Raised when a floating point operation fails. This exception is always defined, |
| 127 | but can only be raised when Python is configured with the |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 128 | ``--with-fpectl`` option, or the :const:`WANT_SIGFPE_HANDLER` symbol is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 129 | defined in the :file:`pyconfig.h` file. |
| 130 | |
| 131 | |
| 132 | .. exception:: GeneratorExit |
| 133 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 134 | Raise when a :term:`generator`\'s :meth:`close` method is called. It |
| 135 | directly inherits from :exc:`BaseException` instead of :exc:`Exception` since |
| 136 | it is technically not an error. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 137 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 138 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 139 | .. exception:: ImportError |
| 140 | |
| 141 | Raised when an :keyword:`import` statement fails to find the module definition |
| 142 | or when a ``from ... import`` fails to find a name that is to be imported. |
| 143 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 144 | |
| 145 | .. exception:: IndexError |
| 146 | |
Georg Brandl | 95817b3 | 2008-05-11 14:30:18 +0000 | [diff] [blame] | 147 | Raised when a sequence subscript is out of range. (Slice indices are |
| 148 | silently truncated to fall in the allowed range; if an index is not an |
| 149 | integer, :exc:`TypeError` is raised.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 150 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 151 | .. XXX xref to sequences |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 152 | |
| 153 | |
| 154 | .. exception:: KeyError |
| 155 | |
| 156 | Raised when a mapping (dictionary) key is not found in the set of existing keys. |
| 157 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 158 | .. XXX xref to mapping objects? |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 159 | |
| 160 | |
| 161 | .. exception:: KeyboardInterrupt |
| 162 | |
| 163 | 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] | 164 | :kbd:`Delete`). During execution, a check for interrupts is made |
| 165 | regularly. The exception inherits from :exc:`BaseException` so as to not be |
| 166 | accidentally caught by code that catches :exc:`Exception` and thus prevent |
| 167 | the interpreter from exiting. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 168 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 169 | |
| 170 | .. exception:: MemoryError |
| 171 | |
| 172 | Raised when an operation runs out of memory but the situation may still be |
| 173 | rescued (by deleting some objects). The associated value is a string indicating |
| 174 | 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] | 175 | underlying memory management architecture (C's :c:func:`malloc` function), the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 176 | interpreter may not always be able to completely recover from this situation; it |
| 177 | nevertheless raises an exception so that a stack traceback can be printed, in |
| 178 | case a run-away program was the cause. |
| 179 | |
| 180 | |
| 181 | .. exception:: NameError |
| 182 | |
| 183 | Raised when a local or global name is not found. This applies only to |
| 184 | unqualified names. The associated value is an error message that includes the |
| 185 | name that could not be found. |
| 186 | |
| 187 | |
| 188 | .. exception:: NotImplementedError |
| 189 | |
| 190 | This exception is derived from :exc:`RuntimeError`. In user defined base |
| 191 | classes, abstract methods should raise this exception when they require derived |
| 192 | classes to override the method. |
| 193 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 194 | |
| 195 | .. exception:: OSError |
| 196 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 197 | .. index:: module: errno |
| 198 | |
Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 199 | This exception is raised when a system function returns a system-related |
| 200 | error, including I/O failures such as "file not found" or "disk full" |
| 201 | (not for illegal argument types or other incidental errors). Often a |
| 202 | subclass of :exc:`OSError` will actually be raised as described in |
| 203 | `OS exceptions`_ below. The :attr:`errno` attribute is a numeric error |
| 204 | code from the C variable :c:data:`errno`. |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 205 | |
Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 206 | Under Windows, the :attr:`winerror` attribute gives you the native |
| 207 | Windows error code. The :attr:`errno` attribute is then an approximate |
| 208 | translation, in POSIX terms, of that native error code. |
| 209 | |
| 210 | Under all platforms, the :attr:`strerror` attribute is the corresponding |
| 211 | error message as provided by the operating system (as formatted by the C |
| 212 | functions :c:func:`perror` under POSIX, and :c:func:`FormatMessage` |
| 213 | Windows). |
| 214 | |
| 215 | For exceptions that involve a file system path (such as :func:`open` or |
| 216 | :func:`os.unlink`), the exception instance will contain an additional |
| 217 | attribute, :attr:`filename`, which is the file name passed to the function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 218 | |
Antoine Pitrou | 195e702 | 2011-10-12 16:46:46 +0200 | [diff] [blame] | 219 | .. versionchanged:: 3.3 |
| 220 | :exc:`EnvironmentError`, :exc:`IOError`, :exc:`WindowsError`, |
| 221 | :exc:`VMSError`, :exc:`socket.error`, :exc:`select.error` and |
| 222 | :exc:`mmap.error` have been merged into :exc:`OSError`. |
| 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 | c4a55fc | 2010-02-06 18:46:57 +0000 | [diff] [blame] | 252 | Raised by built-in function :func:`next` and an :term:`iterator`\'s |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 253 | :meth:`__next__` method to signal that there are no further items to be |
| 254 | produced by the iterator. |
| 255 | |
| 256 | The exception object has a single attribute :attr:`value`, which is |
| 257 | given as an argument when constructing the exception, and defaults |
| 258 | to :const:`None`. |
| 259 | |
| 260 | When a generator function returns, a new :exc:`StopIteration` instance is |
| 261 | raised, and the value returned by the function is used as the |
| 262 | :attr:`value` parameter to the constructor of the exception. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 263 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 264 | |
| 265 | .. exception:: SyntaxError |
| 266 | |
| 267 | Raised when the parser encounters a syntax error. This may occur in an |
| 268 | :keyword:`import` statement, in a call to the built-in functions :func:`exec` |
| 269 | or :func:`eval`, or when reading the initial script or standard input |
| 270 | (also interactively). |
| 271 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 272 | Instances of this class have attributes :attr:`filename`, :attr:`lineno`, |
| 273 | :attr:`offset` and :attr:`text` for easier access to the details. :func:`str` |
| 274 | of the exception instance returns only the message. |
| 275 | |
| 276 | |
Georg Brandl | 0bdfbfa | 2010-12-18 17:51:28 +0000 | [diff] [blame] | 277 | .. exception:: IndentationError |
| 278 | |
| 279 | Base class for syntax errors related to incorrect indentation. This is a |
| 280 | subclass of :exc:`SyntaxError`. |
| 281 | |
| 282 | |
| 283 | .. exception:: TabError |
| 284 | |
| 285 | Raised when indentation contains an inconsistent use of tabs and spaces. |
| 286 | This is a subclass of :exc:`IndentationError`. |
| 287 | |
| 288 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 289 | .. exception:: SystemError |
| 290 | |
| 291 | Raised when the interpreter finds an internal error, but the situation does not |
| 292 | look so serious to cause it to abandon all hope. The associated value is a |
| 293 | string indicating what went wrong (in low-level terms). |
| 294 | |
| 295 | You should report this to the author or maintainer of your Python interpreter. |
| 296 | Be sure to report the version of the Python interpreter (``sys.version``; it is |
| 297 | also printed at the start of an interactive Python session), the exact error |
| 298 | message (the exception's associated value) and if possible the source of the |
| 299 | program that triggered the error. |
| 300 | |
| 301 | |
| 302 | .. exception:: SystemExit |
| 303 | |
| 304 | This exception is raised by the :func:`sys.exit` function. When it is not |
| 305 | handled, the Python interpreter exits; no stack traceback is printed. If the |
Georg Brandl | 95817b3 | 2008-05-11 14:30:18 +0000 | [diff] [blame] | 306 | associated value is an integer, it specifies the system exit status (passed |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 307 | to C's :c:func:`exit` function); if it is ``None``, the exit status is zero; |
Georg Brandl | 95817b3 | 2008-05-11 14:30:18 +0000 | [diff] [blame] | 308 | if it has another type (such as a string), the object's value is printed and |
| 309 | the exit status is one. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 310 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 311 | Instances have an attribute :attr:`code` which is set to the proposed exit |
| 312 | status or error message (defaulting to ``None``). Also, this exception derives |
| 313 | directly from :exc:`BaseException` and not :exc:`Exception`, since it is not |
| 314 | technically an error. |
| 315 | |
| 316 | A call to :func:`sys.exit` is translated into an exception so that clean-up |
| 317 | handlers (:keyword:`finally` clauses of :keyword:`try` statements) can be |
| 318 | executed, and so that a debugger can execute a script without running the risk |
| 319 | of losing control. The :func:`os._exit` function can be used if it is |
| 320 | absolutely positively necessary to exit immediately (for example, in the child |
| 321 | process after a call to :func:`fork`). |
| 322 | |
| 323 | The exception inherits from :exc:`BaseException` instead of :exc:`Exception` so |
| 324 | that it is not accidentally caught by code that catches :exc:`Exception`. This |
| 325 | allows the exception to properly propagate up and cause the interpreter to exit. |
| 326 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 327 | |
| 328 | .. exception:: TypeError |
| 329 | |
| 330 | Raised when an operation or function is applied to an object of inappropriate |
| 331 | type. The associated value is a string giving details about the type mismatch. |
| 332 | |
| 333 | |
| 334 | .. exception:: UnboundLocalError |
| 335 | |
| 336 | Raised when a reference is made to a local variable in a function or method, but |
| 337 | no value has been bound to that variable. This is a subclass of |
| 338 | :exc:`NameError`. |
| 339 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 340 | |
| 341 | .. exception:: UnicodeError |
| 342 | |
| 343 | Raised when a Unicode-related encoding or decoding error occurs. It is a |
| 344 | subclass of :exc:`ValueError`. |
| 345 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 346 | |
| 347 | .. exception:: UnicodeEncodeError |
| 348 | |
| 349 | Raised when a Unicode-related error occurs during encoding. It is a subclass of |
| 350 | :exc:`UnicodeError`. |
| 351 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 352 | |
| 353 | .. exception:: UnicodeDecodeError |
| 354 | |
| 355 | Raised when a Unicode-related error occurs during decoding. It is a subclass of |
| 356 | :exc:`UnicodeError`. |
| 357 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 358 | |
| 359 | .. exception:: UnicodeTranslateError |
| 360 | |
| 361 | Raised when a Unicode-related error occurs during translating. It is a subclass |
| 362 | of :exc:`UnicodeError`. |
| 363 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 364 | |
| 365 | .. exception:: ValueError |
| 366 | |
| 367 | Raised when a built-in operation or function receives an argument that has the |
| 368 | right type but an inappropriate value, and the situation is not described by a |
| 369 | more precise exception such as :exc:`IndexError`. |
| 370 | |
| 371 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 372 | .. exception:: ZeroDivisionError |
| 373 | |
| 374 | Raised when the second argument of a division or modulo operation is zero. The |
| 375 | associated value is a string indicating the type of the operands and the |
| 376 | operation. |
| 377 | |
Georg Brandl | fbd1b22 | 2009-12-29 21:38:35 +0000 | [diff] [blame] | 378 | |
Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 379 | The following exceptions are kept for compatibility with previous versions; |
| 380 | starting from Python 3.3, they are aliases of :exc:`OSError`. |
| 381 | |
| 382 | .. exception:: EnvironmentError |
| 383 | |
| 384 | .. exception:: IOError |
| 385 | |
| 386 | .. exception:: VMSError |
| 387 | |
| 388 | Only available on VMS. |
| 389 | |
| 390 | .. exception:: WindowsError |
| 391 | |
| 392 | Only available on Windows. |
| 393 | |
| 394 | |
| 395 | OS exceptions |
| 396 | ^^^^^^^^^^^^^ |
| 397 | |
| 398 | The following exceptions are subclasses of :exc:`OSError`, they get raised |
| 399 | depending on the system error code. |
| 400 | |
| 401 | .. exception:: BlockingIOError |
| 402 | |
| 403 | Raised when an operation would block on an object (e.g. socket) set |
| 404 | for non-blocking operation. |
| 405 | Corresponds to :c:data:`errno` ``EAGAIN``, ``EALREADY``, |
| 406 | ``EWOULDBLOCK`` and ``EINPROGRESS``. |
| 407 | |
Antoine Pitrou | f55011f | 2011-10-12 18:57:23 +0200 | [diff] [blame] | 408 | In addition to those of :exc:`OSError`, :exc:`BlockingIOError` can have |
| 409 | one more attribute: |
| 410 | |
| 411 | .. attribute:: characters_written |
| 412 | |
| 413 | An integer containing the number of characters written to the stream |
| 414 | before it blocked. This attribute is available when using the |
| 415 | buffered I/O classes from the :mod:`io` module. |
| 416 | |
Antoine Pitrou | f9c7746 | 2011-10-12 16:02:00 +0200 | [diff] [blame] | 417 | .. exception:: ChildProcessError |
| 418 | |
| 419 | Raised when an operation on a child process failed. |
| 420 | Corresponds to :c:data:`errno` ``ECHILD``. |
| 421 | |
| 422 | .. exception:: ConnectionError |
| 423 | |
| 424 | A base class for connection-related issues. Subclasses are |
| 425 | :exc:`BrokenPipeError`, :exc:`ConnectionAbortedError`, |
| 426 | :exc:`ConnectionRefusedError` and :exc:`ConnectionResetError`. |
| 427 | |
| 428 | .. exception:: BrokenPipeError |
| 429 | |
| 430 | A subclass of :exc:`ConnectionError`, raised when trying to write on a |
| 431 | pipe while the other end has been closed, or trying to write on a socket |
| 432 | which has been shutdown for writing. |
| 433 | Corresponds to :c:data:`errno` ``EPIPE`` and ``ESHUTDOWN``. |
| 434 | |
| 435 | .. exception:: ConnectionAbortedError |
| 436 | |
| 437 | A subclass of :exc:`ConnectionError`, raised when a connection attempt |
| 438 | is aborted by the peer. |
| 439 | Corresponds to :c:data:`errno` ``ECONNABORTED``. |
| 440 | |
| 441 | .. exception:: ConnectionRefusedError |
| 442 | |
| 443 | A subclass of :exc:`ConnectionError`, raised when a connection attempt |
| 444 | is refused by the peer. |
| 445 | Corresponds to :c:data:`errno` ``ECONNREFUSED``. |
| 446 | |
| 447 | .. exception:: ConnectionResetError |
| 448 | |
| 449 | A subclass of :exc:`ConnectionError`, raised when a connection is |
| 450 | reset by the peer. |
| 451 | Corresponds to :c:data:`errno` ``ECONNRESET``. |
| 452 | |
| 453 | .. exception:: FileExistsError |
| 454 | |
| 455 | Raised when trying to create a file or directory which already exists. |
| 456 | Corresponds to :c:data:`errno` ``EEXIST``. |
| 457 | |
| 458 | .. exception:: FileNotFoundError |
| 459 | |
| 460 | Raised when a file or directory is requested but doesn't exist. |
| 461 | Corresponds to :c:data:`errno` ``ENOENT``. |
| 462 | |
| 463 | .. exception:: InterruptedError |
| 464 | |
| 465 | Raised when a system call is interrupted by an incoming signal. |
| 466 | Corresponds to :c:data:`errno` ``EEINTR``. |
| 467 | |
| 468 | .. exception:: IsADirectoryError |
| 469 | |
| 470 | Raised when a file operation (such as :func:`os.remove`) is requested |
| 471 | on a directory. |
| 472 | Corresponds to :c:data:`errno` ``EISDIR``. |
| 473 | |
| 474 | .. exception:: NotADirectoryError |
| 475 | |
| 476 | Raised when a directory operation (such as :func:`os.listdir`) is requested |
| 477 | on something which is not a directory. |
| 478 | Corresponds to :c:data:`errno` ``ENOTDIR``. |
| 479 | |
| 480 | .. exception:: PermissionError |
| 481 | |
| 482 | Raised when trying to run an operation without the adequate access |
| 483 | rights - for example filesystem permissions. |
| 484 | Corresponds to :c:data:`errno` ``EACCES`` and ``EPERM``. |
| 485 | |
| 486 | .. exception:: ProcessLookupError |
| 487 | |
| 488 | Raised when a given process doesn't exist. |
| 489 | Corresponds to :c:data:`errno` ``ESRCH``. |
| 490 | |
| 491 | .. exception:: TimeoutError |
| 492 | |
| 493 | Raised when a system function timed out at the system level. |
| 494 | Corresponds to :c:data:`errno` ``ETIMEDOUT``. |
| 495 | |
| 496 | .. versionadded:: 3.3 |
| 497 | All the above :exc:`OSError` subclasses were added. |
| 498 | |
| 499 | |
| 500 | .. seealso:: |
| 501 | |
| 502 | :pep:`3151` - Reworking the OS and IO exception hierarchy |
| 503 | PEP written and implemented by Antoine Pitrou. |
| 504 | |
| 505 | |
| 506 | Warnings |
| 507 | -------- |
| 508 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 509 | The following exceptions are used as warning categories; see the :mod:`warnings` |
| 510 | module for more information. |
| 511 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 512 | .. exception:: Warning |
| 513 | |
| 514 | Base class for warning categories. |
| 515 | |
| 516 | |
| 517 | .. exception:: UserWarning |
| 518 | |
| 519 | Base class for warnings generated by user code. |
| 520 | |
| 521 | |
| 522 | .. exception:: DeprecationWarning |
| 523 | |
| 524 | Base class for warnings about deprecated features. |
| 525 | |
| 526 | |
| 527 | .. exception:: PendingDeprecationWarning |
| 528 | |
| 529 | Base class for warnings about features which will be deprecated in the future. |
| 530 | |
| 531 | |
| 532 | .. exception:: SyntaxWarning |
| 533 | |
| 534 | Base class for warnings about dubious syntax |
| 535 | |
| 536 | |
| 537 | .. exception:: RuntimeWarning |
| 538 | |
| 539 | Base class for warnings about dubious runtime behavior. |
| 540 | |
| 541 | |
| 542 | .. exception:: FutureWarning |
| 543 | |
| 544 | Base class for warnings about constructs that will change semantically in the |
| 545 | future. |
| 546 | |
| 547 | |
| 548 | .. exception:: ImportWarning |
| 549 | |
| 550 | Base class for warnings about probable mistakes in module imports. |
| 551 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 552 | |
| 553 | .. exception:: UnicodeWarning |
| 554 | |
| 555 | Base class for warnings related to Unicode. |
| 556 | |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 557 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 558 | .. exception:: BytesWarning |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 559 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 560 | Base class for warnings related to :class:`bytes` and :class:`buffer`. |
| 561 | |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 562 | |
| 563 | .. exception:: ResourceWarning |
| 564 | |
| 565 | Base class for warnings related to resource usage. |
| 566 | |
| 567 | .. versionadded:: 3.2 |
| 568 | |
| 569 | |
| 570 | |
Alexandre Vassalotti | c22c6f2 | 2009-07-21 00:51:58 +0000 | [diff] [blame] | 571 | Exception hierarchy |
| 572 | ------------------- |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 573 | |
| 574 | The class hierarchy for built-in exceptions is: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 575 | |
| 576 | .. literalinclude:: ../../Lib/test/exception_hierarchy.txt |