blob: 74d6fcb78c273fe71d5c3e627bc110a02bb497be [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. _bltin-exceptions:
2
3Built-in Exceptions
4===================
5
Georg Brandl116aa622007-08-15 14:28:22 +00006.. index::
7 statement: try
8 statement: except
9
Georg Brandlfbd1b222009-12-29 21:38:35 +000010In 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 Brandl116aa622007-08-15 14:28:22 +000012clause that mentions a particular class, that clause also handles any exception
13classes derived from that class (but not exception classes from which *it* is
14derived). Two exception classes that are not related via subclassing are never
15equivalent, even if they have the same name.
16
17.. index:: statement: raise
18
19The built-in exceptions listed below can be generated by the interpreter or
20built-in functions. Except where mentioned, they have an "associated value"
Georg Brandlfb6fd5d2011-01-07 18:28:45 +000021indicating the detailed cause of the error. This may be a string or a tuple of
22several items of information (e.g., an error code and a string explaining the
23code). The associated value is usually passed as arguments to the exception
24class's constructor.
Georg Brandl116aa622007-08-15 14:28:22 +000025
26User code can raise built-in exceptions. This can be used to test an exception
27handler or to report an error condition "just like" the situation in which the
28interpreter raises the same exception; but beware that there is nothing to
29prevent user code from raising an inappropriate error.
30
Mark Dickinsonabf079d2014-04-14 11:20:12 -040031The built-in exception classes can be subclassed to define new exceptions;
32programmers are encouraged to derive new exceptions from the :exc:`Exception`
33class or one of its subclasses, and not from :exc:`BaseException`. More
34information on defining exceptions is available in the Python Tutorial under
Georg Brandl116aa622007-08-15 14:28:22 +000035:ref:`tut-userexceptions`.
36
Nick Coghlanab7bf212012-02-26 17:49:52 +100037When raising (or re-raising) an exception in an :keyword:`except` clause
38:attr:`__context__` is automatically set to the last exception caught; if the
39new exception is not handled the traceback that is eventually displayed will
40include the originating exception(s) and the final exception.
41
Nick Coghlan0eee97c2012-12-09 16:21:46 +100042When raising a new exception (rather than using a bare ``raise`` to re-raise
43the exception currently being handled), the implicit exception context can be
44supplemented with an explicit cause by using :keyword:`from` with
45:keyword:`raise`::
46
47 raise new_exc from original_exc
48
49The expression following :keyword:`from` must be an exception or ``None``. It
50will be set as :attr:`__cause__` on the raised exception. Setting
51:attr:`__cause__` also implicitly sets the :attr:`__suppress_context__`
52attribute to ``True``, so that using ``raise new_exc from None``
53effectively replaces the old exception with the new one for display
54purposes (e.g. converting :exc:`KeyError` to :exc:`AttributeError`, while
55leaving the old exception available in :attr:`__context__` for introspection
56when debugging.
Nick Coghlanab7bf212012-02-26 17:49:52 +100057
Nick Coghlan8e18fc82012-12-08 21:39:24 +100058The default traceback display code shows these chained exceptions in
59addition to the traceback for the exception itself. An explicitly chained
60exception in :attr:`__cause__` is always shown when present. An implicitly
61chained exception in :attr:`__context__` is shown only if :attr:`__cause__`
Nick Coghlan0eee97c2012-12-09 16:21:46 +100062is :const:`None` and :attr:`__suppress_context__` is false.
Nick Coghlan8e18fc82012-12-08 21:39:24 +100063
64In either case, the exception itself is always shown after any chained
65exceptions so that the final line of the traceback always shows the last
66exception that was raised.
Nick Coghlanab7bf212012-02-26 17:49:52 +100067
Antoine Pitrouf9c77462011-10-12 16:02:00 +020068
69Base classes
70------------
71
Georg Brandlfbd1b222009-12-29 21:38:35 +000072The following exceptions are used mostly as base classes for other exceptions.
Georg Brandl116aa622007-08-15 14:28:22 +000073
Georg Brandl116aa622007-08-15 14:28:22 +000074.. exception:: BaseException
75
76 The base class for all built-in exceptions. It is not meant to be directly
Georg Brandlfb6fd5d2011-01-07 18:28:45 +000077 inherited by user-defined classes (for that, use :exc:`Exception`). If
Amaury Forgeot d'Arc3b1acf12011-11-22 19:34:08 +010078 :func:`str` is called on an instance of this class, the representation of
79 the argument(s) to the instance are returned, or the empty string when
80 there were no arguments.
Georg Brandlfb6fd5d2011-01-07 18:28:45 +000081
82 .. attribute:: args
83
84 The tuple of arguments given to the exception constructor. Some built-in
Andrew Svetlov5898d4f2014-04-01 00:44:13 +030085 exceptions (like :exc:`OSError`) expect a certain number of arguments and
Georg Brandlfb6fd5d2011-01-07 18:28:45 +000086 assign a special meaning to the elements of this tuple, while others are
87 usually called only with a single string giving an error message.
88
89 .. method:: with_traceback(tb)
90
91 This method sets *tb* as the new traceback for the exception and returns
92 the exception object. It is usually used in exception handling code like
93 this::
94
95 try:
96 ...
97 except SomeException:
98 tb = sys.exc_info()[2]
99 raise OtherException(...).with_traceback(tb)
Georg Brandl116aa622007-08-15 14:28:22 +0000100
Georg Brandl116aa622007-08-15 14:28:22 +0000101
102.. exception:: Exception
103
104 All built-in, non-system-exiting exceptions are derived from this class. All
105 user-defined exceptions should also be derived from this class.
106
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108.. exception:: ArithmeticError
109
110 The base class for those built-in exceptions that are raised for various
111 arithmetic errors: :exc:`OverflowError`, :exc:`ZeroDivisionError`,
112 :exc:`FloatingPointError`.
113
114
Georg Brandl0bdfbfa2010-12-18 17:51:28 +0000115.. exception:: BufferError
116
117 Raised when a :ref:`buffer <bufferobjects>` related operation cannot be
118 performed.
119
120
Georg Brandl116aa622007-08-15 14:28:22 +0000121.. exception:: LookupError
122
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000123 The base class for the exceptions that are raised when a key or index used on
124 a mapping or sequence is invalid: :exc:`IndexError`, :exc:`KeyError`. This
125 can be raised directly by :func:`codecs.lookup`.
Georg Brandl116aa622007-08-15 14:28:22 +0000126
127
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200128Concrete exceptions
129-------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000130
Georg Brandlfbd1b222009-12-29 21:38:35 +0000131The following exceptions are the exceptions that are usually raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000132
133.. exception:: AssertionError
134
135 .. index:: statement: assert
136
137 Raised when an :keyword:`assert` statement fails.
138
139
140.. exception:: AttributeError
141
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000142 Raised when an attribute reference (see :ref:`attribute-references`) or
143 assignment fails. (When an object does not support attribute references or
144 attribute assignments at all, :exc:`TypeError` is raised.)
Georg Brandl116aa622007-08-15 14:28:22 +0000145
146
147.. exception:: EOFError
148
Serhiy Storchakabfdcd432013-10-13 23:09:14 +0300149 Raised when the :func:`input` function hits an end-of-file condition (EOF)
150 without reading any data. (N.B.: the :meth:`io.IOBase.read` and
151 :meth:`io.IOBase.readline` methods return an empty string when they hit EOF.)
Georg Brandl116aa622007-08-15 14:28:22 +0000152
153
154.. exception:: FloatingPointError
155
156 Raised when a floating point operation fails. This exception is always defined,
157 but can only be raised when Python is configured with the
Éric Araujo713d3032010-11-18 16:38:46 +0000158 ``--with-fpectl`` option, or the :const:`WANT_SIGFPE_HANDLER` symbol is
Georg Brandl116aa622007-08-15 14:28:22 +0000159 defined in the :file:`pyconfig.h` file.
160
161
162.. exception:: GeneratorExit
163
Benjamin Peterson222ef822014-04-07 19:34:33 -0400164 Raised when a :term:`generator`\'s :meth:`close` method is called. It
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000165 directly inherits from :exc:`BaseException` instead of :exc:`Exception` since
166 it is technically not an error.
Georg Brandl116aa622007-08-15 14:28:22 +0000167
Georg Brandl116aa622007-08-15 14:28:22 +0000168
Georg Brandl116aa622007-08-15 14:28:22 +0000169.. exception:: ImportError
170
Brett Cannon679ecb52013-07-04 17:51:50 -0400171 Raised when an :keyword:`import` statement fails to find the module definition
172 or when a ``from ... import`` fails to find a name that is to be imported.
Georg Brandl116aa622007-08-15 14:28:22 +0000173
Brett Cannon79ec55e2012-04-12 20:24:54 -0400174 The :attr:`name` and :attr:`path` attributes can be set using keyword-only
175 arguments to the constructor. When set they represent the name of the module
176 that was attempted to be imported and the path to any file which triggered
177 the exception, respectively.
178
179 .. versionchanged:: 3.3
180 Added the :attr:`name` and :attr:`path` attributes.
181
Georg Brandl116aa622007-08-15 14:28:22 +0000182
183.. exception:: IndexError
184
Georg Brandl95817b32008-05-11 14:30:18 +0000185 Raised when a sequence subscript is out of range. (Slice indices are
186 silently truncated to fall in the allowed range; if an index is not an
187 integer, :exc:`TypeError` is raised.)
Georg Brandl116aa622007-08-15 14:28:22 +0000188
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000189 .. XXX xref to sequences
Georg Brandl116aa622007-08-15 14:28:22 +0000190
191
192.. exception:: KeyError
193
194 Raised when a mapping (dictionary) key is not found in the set of existing keys.
195
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000196 .. XXX xref to mapping objects?
Georg Brandl116aa622007-08-15 14:28:22 +0000197
198
199.. exception:: KeyboardInterrupt
200
201 Raised when the user hits the interrupt key (normally :kbd:`Control-C` or
Georg Brandl81ac1ce2007-08-31 17:17:17 +0000202 :kbd:`Delete`). During execution, a check for interrupts is made
203 regularly. The exception inherits from :exc:`BaseException` so as to not be
204 accidentally caught by code that catches :exc:`Exception` and thus prevent
205 the interpreter from exiting.
Georg Brandl116aa622007-08-15 14:28:22 +0000206
Georg Brandl116aa622007-08-15 14:28:22 +0000207
208.. exception:: MemoryError
209
210 Raised when an operation runs out of memory but the situation may still be
211 rescued (by deleting some objects). The associated value is a string indicating
212 what kind of (internal) operation ran out of memory. Note that because of the
Georg Brandl60203b42010-10-06 10:11:56 +0000213 underlying memory management architecture (C's :c:func:`malloc` function), the
Georg Brandl116aa622007-08-15 14:28:22 +0000214 interpreter may not always be able to completely recover from this situation; it
215 nevertheless raises an exception so that a stack traceback can be printed, in
216 case a run-away program was the cause.
217
218
219.. exception:: NameError
220
221 Raised when a local or global name is not found. This applies only to
222 unqualified names. The associated value is an error message that includes the
223 name that could not be found.
224
225
226.. exception:: NotImplementedError
227
228 This exception is derived from :exc:`RuntimeError`. In user defined base
229 classes, abstract methods should raise this exception when they require derived
230 classes to override the method.
231
Georg Brandl116aa622007-08-15 14:28:22 +0000232
233.. exception:: OSError
234
Christian Heimesa62da1d2008-01-12 19:39:10 +0000235 .. index:: module: errno
236
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200237 This exception is raised when a system function returns a system-related
238 error, including I/O failures such as "file not found" or "disk full"
239 (not for illegal argument types or other incidental errors). Often a
240 subclass of :exc:`OSError` will actually be raised as described in
241 `OS exceptions`_ below. The :attr:`errno` attribute is a numeric error
242 code from the C variable :c:data:`errno`.
Christian Heimesa62da1d2008-01-12 19:39:10 +0000243
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200244 Under Windows, the :attr:`winerror` attribute gives you the native
245 Windows error code. The :attr:`errno` attribute is then an approximate
246 translation, in POSIX terms, of that native error code.
247
248 Under all platforms, the :attr:`strerror` attribute is the corresponding
249 error message as provided by the operating system (as formatted by the C
250 functions :c:func:`perror` under POSIX, and :c:func:`FormatMessage`
251 Windows).
252
253 For exceptions that involve a file system path (such as :func:`open` or
254 :func:`os.unlink`), the exception instance will contain an additional
255 attribute, :attr:`filename`, which is the file name passed to the function.
Larry Hastingsb0827312014-02-09 22:05:19 -0800256 For functions that involve two file system paths (such as
257 :func:`os.rename`), the exception instance will contain a second
258 :attr:`filename2` attribute corresponding to the second file name passed
259 to the function.
260
Georg Brandl116aa622007-08-15 14:28:22 +0000261
Antoine Pitrou195e7022011-10-12 16:46:46 +0200262 .. versionchanged:: 3.3
263 :exc:`EnvironmentError`, :exc:`IOError`, :exc:`WindowsError`,
264 :exc:`VMSError`, :exc:`socket.error`, :exc:`select.error` and
265 :exc:`mmap.error` have been merged into :exc:`OSError`.
266
Victor Stinner292c8352012-10-30 02:17:38 +0100267 .. versionchanged:: 3.4
Victor Stinner292c8352012-10-30 02:17:38 +0100268 The :attr:`filename` attribute is now the original file name passed to
269 the function, instead of the name encoded to or decoded from the
Larry Hastingsb0827312014-02-09 22:05:19 -0800270 filesystem encoding. Also, the :attr:`filename2` attribute was added.
Victor Stinner292c8352012-10-30 02:17:38 +0100271
Georg Brandl116aa622007-08-15 14:28:22 +0000272
273.. exception:: OverflowError
274
275 Raised when the result of an arithmetic operation is too large to be
Georg Brandlba956ae2007-11-29 17:24:34 +0000276 represented. This cannot occur for integers (which would rather raise
Terry Jan Reedyb6d1f482014-06-16 03:31:00 -0400277 :exc:`MemoryError` than give up). However, for historical reasons,
278 OverflowError is sometimes raised for integers that are outside a required
279 range. Because of the lack of standardization of floating point exception
280 handling in C, most floating point operations are not checked.
Georg Brandl116aa622007-08-15 14:28:22 +0000281
282
283.. exception:: ReferenceError
284
285 This exception is raised when a weak reference proxy, created by the
286 :func:`weakref.proxy` function, is used to access an attribute of the referent
287 after it has been garbage collected. For more information on weak references,
288 see the :mod:`weakref` module.
289
Georg Brandl116aa622007-08-15 14:28:22 +0000290
291.. exception:: RuntimeError
292
293 Raised when an error is detected that doesn't fall in any of the other
294 categories. The associated value is a string indicating what precisely went
Antoine Pitrou9527f162013-11-25 19:08:32 +0100295 wrong.
Georg Brandl116aa622007-08-15 14:28:22 +0000296
297
298.. exception:: StopIteration
299
Georg Brandlc4a55fc2010-02-06 18:46:57 +0000300 Raised by built-in function :func:`next` and an :term:`iterator`\'s
Ezio Melotti1dd7c302012-10-12 13:45:38 +0300301 :meth:`~iterator.__next__` method to signal that there are no further
302 items produced by the iterator.
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000303
304 The exception object has a single attribute :attr:`value`, which is
305 given as an argument when constructing the exception, and defaults
306 to :const:`None`.
307
308 When a generator function returns, a new :exc:`StopIteration` instance is
309 raised, and the value returned by the function is used as the
310 :attr:`value` parameter to the constructor of the exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000311
Nick Coghlan0ed80192012-01-14 14:43:24 +1000312 .. versionchanged:: 3.3
313 Added ``value`` attribute and the ability for generator functions to
314 use it to return a value.
Georg Brandl116aa622007-08-15 14:28:22 +0000315
316.. exception:: SyntaxError
317
318 Raised when the parser encounters a syntax error. This may occur in an
319 :keyword:`import` statement, in a call to the built-in functions :func:`exec`
320 or :func:`eval`, or when reading the initial script or standard input
321 (also interactively).
322
Georg Brandl116aa622007-08-15 14:28:22 +0000323 Instances of this class have attributes :attr:`filename`, :attr:`lineno`,
324 :attr:`offset` and :attr:`text` for easier access to the details. :func:`str`
325 of the exception instance returns only the message.
326
327
Georg Brandl0bdfbfa2010-12-18 17:51:28 +0000328.. exception:: IndentationError
329
330 Base class for syntax errors related to incorrect indentation. This is a
331 subclass of :exc:`SyntaxError`.
332
333
334.. exception:: TabError
335
336 Raised when indentation contains an inconsistent use of tabs and spaces.
337 This is a subclass of :exc:`IndentationError`.
338
339
Georg Brandl116aa622007-08-15 14:28:22 +0000340.. exception:: SystemError
341
342 Raised when the interpreter finds an internal error, but the situation does not
343 look so serious to cause it to abandon all hope. The associated value is a
344 string indicating what went wrong (in low-level terms).
345
346 You should report this to the author or maintainer of your Python interpreter.
347 Be sure to report the version of the Python interpreter (``sys.version``; it is
348 also printed at the start of an interactive Python session), the exact error
349 message (the exception's associated value) and if possible the source of the
350 program that triggered the error.
351
352
353.. exception:: SystemExit
354
355 This exception is raised by the :func:`sys.exit` function. When it is not
356 handled, the Python interpreter exits; no stack traceback is printed. If the
Georg Brandl95817b32008-05-11 14:30:18 +0000357 associated value is an integer, it specifies the system exit status (passed
Georg Brandl60203b42010-10-06 10:11:56 +0000358 to C's :c:func:`exit` function); if it is ``None``, the exit status is zero;
Georg Brandl95817b32008-05-11 14:30:18 +0000359 if it has another type (such as a string), the object's value is printed and
360 the exit status is one.
Georg Brandl116aa622007-08-15 14:28:22 +0000361
Georg Brandlf24c1412013-10-08 21:43:39 +0200362 Instances have an attribute :attr:`!code` which is set to the proposed exit
Georg Brandl116aa622007-08-15 14:28:22 +0000363 status or error message (defaulting to ``None``). Also, this exception derives
364 directly from :exc:`BaseException` and not :exc:`Exception`, since it is not
365 technically an error.
366
367 A call to :func:`sys.exit` is translated into an exception so that clean-up
368 handlers (:keyword:`finally` clauses of :keyword:`try` statements) can be
369 executed, and so that a debugger can execute a script without running the risk
370 of losing control. The :func:`os._exit` function can be used if it is
371 absolutely positively necessary to exit immediately (for example, in the child
Serhiy Storchakabfdcd432013-10-13 23:09:14 +0300372 process after a call to :func:`os.fork`).
Georg Brandl116aa622007-08-15 14:28:22 +0000373
374 The exception inherits from :exc:`BaseException` instead of :exc:`Exception` so
375 that it is not accidentally caught by code that catches :exc:`Exception`. This
376 allows the exception to properly propagate up and cause the interpreter to exit.
377
Georg Brandl116aa622007-08-15 14:28:22 +0000378
379.. exception:: TypeError
380
381 Raised when an operation or function is applied to an object of inappropriate
382 type. The associated value is a string giving details about the type mismatch.
383
384
385.. exception:: UnboundLocalError
386
387 Raised when a reference is made to a local variable in a function or method, but
388 no value has been bound to that variable. This is a subclass of
389 :exc:`NameError`.
390
Georg Brandl116aa622007-08-15 14:28:22 +0000391
392.. exception:: UnicodeError
393
394 Raised when a Unicode-related encoding or decoding error occurs. It is a
395 subclass of :exc:`ValueError`.
396
Benjamin Peterson78f7e3a2012-12-02 11:33:06 -0500397 :exc:`UnicodeError` has attributes that describe the encoding or decoding
398 error. For example, ``err.object[err.start:err.end]`` gives the particular
399 invalid input that the codec failed on.
400
401 .. attribute:: encoding
402
403 The name of the encoding that raised the error.
404
405 .. attribute:: reason
406
407 A string describing the specific codec error.
408
409 .. attribute:: object
410
411 The object the codec was attempting to encode or decode.
412
413 .. attribute:: start
414
415 The first index of invalid data in :attr:`object`.
416
417 .. attribute:: end
418
419 The index after the last invalid data in :attr:`object`.
420
Georg Brandl116aa622007-08-15 14:28:22 +0000421
422.. exception:: UnicodeEncodeError
423
424 Raised when a Unicode-related error occurs during encoding. It is a subclass of
425 :exc:`UnicodeError`.
426
Georg Brandl116aa622007-08-15 14:28:22 +0000427
428.. exception:: UnicodeDecodeError
429
430 Raised when a Unicode-related error occurs during decoding. It is a subclass of
431 :exc:`UnicodeError`.
432
Georg Brandl116aa622007-08-15 14:28:22 +0000433
434.. exception:: UnicodeTranslateError
435
436 Raised when a Unicode-related error occurs during translating. It is a subclass
437 of :exc:`UnicodeError`.
438
Georg Brandl116aa622007-08-15 14:28:22 +0000439
440.. exception:: ValueError
441
442 Raised when a built-in operation or function receives an argument that has the
443 right type but an inappropriate value, and the situation is not described by a
444 more precise exception such as :exc:`IndexError`.
445
446
Georg Brandl116aa622007-08-15 14:28:22 +0000447.. exception:: ZeroDivisionError
448
449 Raised when the second argument of a division or modulo operation is zero. The
450 associated value is a string indicating the type of the operands and the
451 operation.
452
Georg Brandlfbd1b222009-12-29 21:38:35 +0000453
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200454The following exceptions are kept for compatibility with previous versions;
455starting from Python 3.3, they are aliases of :exc:`OSError`.
456
457.. exception:: EnvironmentError
458
459.. exception:: IOError
460
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200461.. exception:: WindowsError
462
463 Only available on Windows.
464
465
466OS exceptions
467^^^^^^^^^^^^^
468
469The following exceptions are subclasses of :exc:`OSError`, they get raised
470depending on the system error code.
471
472.. exception:: BlockingIOError
473
474 Raised when an operation would block on an object (e.g. socket) set
475 for non-blocking operation.
476 Corresponds to :c:data:`errno` ``EAGAIN``, ``EALREADY``,
477 ``EWOULDBLOCK`` and ``EINPROGRESS``.
478
Antoine Pitrouf55011f2011-10-12 18:57:23 +0200479 In addition to those of :exc:`OSError`, :exc:`BlockingIOError` can have
480 one more attribute:
481
482 .. attribute:: characters_written
483
484 An integer containing the number of characters written to the stream
485 before it blocked. This attribute is available when using the
486 buffered I/O classes from the :mod:`io` module.
487
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200488.. exception:: ChildProcessError
489
490 Raised when an operation on a child process failed.
491 Corresponds to :c:data:`errno` ``ECHILD``.
492
493.. exception:: ConnectionError
494
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300495 A base class for connection-related issues.
496
497 Subclasses are :exc:`BrokenPipeError`, :exc:`ConnectionAbortedError`,
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200498 :exc:`ConnectionRefusedError` and :exc:`ConnectionResetError`.
499
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300500.. exception:: BrokenPipeError
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200501
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300502 A subclass of :exc:`ConnectionError`, raised when trying to write on a
503 pipe while the other end has been closed, or trying to write on a socket
504 which has been shutdown for writing.
505 Corresponds to :c:data:`errno` ``EPIPE`` and ``ESHUTDOWN``.
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200506
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300507.. exception:: ConnectionAbortedError
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200508
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300509 A subclass of :exc:`ConnectionError`, raised when a connection attempt
510 is aborted by the peer.
511 Corresponds to :c:data:`errno` ``ECONNABORTED``.
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200512
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300513.. exception:: ConnectionRefusedError
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200514
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300515 A subclass of :exc:`ConnectionError`, raised when a connection attempt
516 is refused by the peer.
517 Corresponds to :c:data:`errno` ``ECONNREFUSED``.
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200518
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300519.. exception:: ConnectionResetError
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200520
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300521 A subclass of :exc:`ConnectionError`, raised when a connection is
522 reset by the peer.
523 Corresponds to :c:data:`errno` ``ECONNRESET``.
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200524
525.. exception:: FileExistsError
526
527 Raised when trying to create a file or directory which already exists.
528 Corresponds to :c:data:`errno` ``EEXIST``.
529
530.. exception:: FileNotFoundError
531
532 Raised when a file or directory is requested but doesn't exist.
533 Corresponds to :c:data:`errno` ``ENOENT``.
534
535.. exception:: InterruptedError
536
537 Raised when a system call is interrupted by an incoming signal.
Andrew Svetlov73a5a122012-12-05 11:12:14 +0200538 Corresponds to :c:data:`errno` ``EINTR``.
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200539
540.. exception:: IsADirectoryError
541
542 Raised when a file operation (such as :func:`os.remove`) is requested
543 on a directory.
544 Corresponds to :c:data:`errno` ``EISDIR``.
545
546.. exception:: NotADirectoryError
547
548 Raised when a directory operation (such as :func:`os.listdir`) is requested
549 on something which is not a directory.
550 Corresponds to :c:data:`errno` ``ENOTDIR``.
551
552.. exception:: PermissionError
553
554 Raised when trying to run an operation without the adequate access
555 rights - for example filesystem permissions.
556 Corresponds to :c:data:`errno` ``EACCES`` and ``EPERM``.
557
558.. exception:: ProcessLookupError
559
560 Raised when a given process doesn't exist.
561 Corresponds to :c:data:`errno` ``ESRCH``.
562
563.. exception:: TimeoutError
564
565 Raised when a system function timed out at the system level.
566 Corresponds to :c:data:`errno` ``ETIMEDOUT``.
567
568.. versionadded:: 3.3
569 All the above :exc:`OSError` subclasses were added.
570
571
572.. seealso::
573
574 :pep:`3151` - Reworking the OS and IO exception hierarchy
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200575
576
577Warnings
578--------
579
Georg Brandl116aa622007-08-15 14:28:22 +0000580The following exceptions are used as warning categories; see the :mod:`warnings`
581module for more information.
582
Georg Brandl116aa622007-08-15 14:28:22 +0000583.. exception:: Warning
584
585 Base class for warning categories.
586
587
588.. exception:: UserWarning
589
590 Base class for warnings generated by user code.
591
592
593.. exception:: DeprecationWarning
594
595 Base class for warnings about deprecated features.
596
597
598.. exception:: PendingDeprecationWarning
599
600 Base class for warnings about features which will be deprecated in the future.
601
602
603.. exception:: SyntaxWarning
604
605 Base class for warnings about dubious syntax
606
607
608.. exception:: RuntimeWarning
609
610 Base class for warnings about dubious runtime behavior.
611
612
613.. exception:: FutureWarning
614
615 Base class for warnings about constructs that will change semantically in the
616 future.
617
618
619.. exception:: ImportWarning
620
621 Base class for warnings about probable mistakes in module imports.
622
Georg Brandl116aa622007-08-15 14:28:22 +0000623
624.. exception:: UnicodeWarning
625
626 Base class for warnings related to Unicode.
627
Georg Brandl08be72d2010-10-24 15:11:22 +0000628
Guido van Rossum98297ee2007-11-06 21:34:58 +0000629.. exception:: BytesWarning
Georg Brandl116aa622007-08-15 14:28:22 +0000630
Serhiy Storchakabfdcd432013-10-13 23:09:14 +0300631 Base class for warnings related to :class:`bytes` and :class:`bytearray`.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000632
Georg Brandl08be72d2010-10-24 15:11:22 +0000633
634.. exception:: ResourceWarning
635
636 Base class for warnings related to resource usage.
637
638 .. versionadded:: 3.2
639
640
641
Alexandre Vassalottic22c6f22009-07-21 00:51:58 +0000642Exception hierarchy
643-------------------
Guido van Rossum98297ee2007-11-06 21:34:58 +0000644
645The class hierarchy for built-in exceptions is:
Georg Brandl116aa622007-08-15 14:28:22 +0000646
647.. literalinclude:: ../../Lib/test/exception_hierarchy.txt