blob: 46116890de210fde4b248708f8a1481d4eedf043 [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
31The built-in exception classes can be sub-classed to define new exceptions;
32programmers are encouraged to at least derive new exceptions from the
33:exc:`Exception` class and not :exc:`BaseException`. More information on
34defining exceptions is available in the Python Tutorial under
35: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 Coghlan8e18fc82012-12-08 21:39:24 +100042When raising a new exception (rather than using to bare ``raise`` to re-raise
43the exception currently being handled), the implicit exception chain can be
44made explicit by using :keyword:`from` with :keyword:`raise`. The single
45argument to :keyword:`from` must be an exception or ``None``. It will be set
46as :attr:`__cause__` on the raised exception. Setting :attr:`__cause__`
47also implicitly sets the :attr:`__suppress_context__` attribute to
48``True``.
Nick Coghlanab7bf212012-02-26 17:49:52 +100049
Nick Coghlan8e18fc82012-12-08 21:39:24 +100050The default traceback display code shows these chained exceptions in
51addition to the traceback for the exception itself. An explicitly chained
52exception in :attr:`__cause__` is always shown when present. An implicitly
53chained exception in :attr:`__context__` is shown only if :attr:`__cause__`
54is not set and :attr:`__suppress_context__` is false.
55
56In either case, the exception itself is always shown after any chained
57exceptions so that the final line of the traceback always shows the last
58exception that was raised.
Nick Coghlanab7bf212012-02-26 17:49:52 +100059
Antoine Pitrouf9c77462011-10-12 16:02:00 +020060
61Base classes
62------------
63
Georg Brandlfbd1b222009-12-29 21:38:35 +000064The following exceptions are used mostly as base classes for other exceptions.
Georg Brandl116aa622007-08-15 14:28:22 +000065
Georg Brandl116aa622007-08-15 14:28:22 +000066.. exception:: BaseException
67
68 The base class for all built-in exceptions. It is not meant to be directly
Georg Brandlfb6fd5d2011-01-07 18:28:45 +000069 inherited by user-defined classes (for that, use :exc:`Exception`). If
Amaury Forgeot d'Arc3b1acf12011-11-22 19:34:08 +010070 :func:`str` is called on an instance of this class, the representation of
71 the argument(s) to the instance are returned, or the empty string when
72 there were no arguments.
Georg Brandlfb6fd5d2011-01-07 18:28:45 +000073
74 .. attribute:: args
75
76 The tuple of arguments given to the exception constructor. Some built-in
77 exceptions (like :exc:`IOError`) expect a certain number of arguments and
78 assign a special meaning to the elements of this tuple, while others are
79 usually called only with a single string giving an error message.
80
81 .. method:: with_traceback(tb)
82
83 This method sets *tb* as the new traceback for the exception and returns
84 the exception object. It is usually used in exception handling code like
85 this::
86
87 try:
88 ...
89 except SomeException:
90 tb = sys.exc_info()[2]
91 raise OtherException(...).with_traceback(tb)
Georg Brandl116aa622007-08-15 14:28:22 +000092
Georg Brandl116aa622007-08-15 14:28:22 +000093
94.. exception:: Exception
95
96 All built-in, non-system-exiting exceptions are derived from this class. All
97 user-defined exceptions should also be derived from this class.
98
Georg Brandl116aa622007-08-15 14:28:22 +000099
100.. exception:: ArithmeticError
101
102 The base class for those built-in exceptions that are raised for various
103 arithmetic errors: :exc:`OverflowError`, :exc:`ZeroDivisionError`,
104 :exc:`FloatingPointError`.
105
106
Georg Brandl0bdfbfa2010-12-18 17:51:28 +0000107.. exception:: BufferError
108
109 Raised when a :ref:`buffer <bufferobjects>` related operation cannot be
110 performed.
111
112
Georg Brandl116aa622007-08-15 14:28:22 +0000113.. exception:: LookupError
114
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000115 The base class for the exceptions that are raised when a key or index used on
116 a mapping or sequence is invalid: :exc:`IndexError`, :exc:`KeyError`. This
117 can be raised directly by :func:`codecs.lookup`.
Georg Brandl116aa622007-08-15 14:28:22 +0000118
119
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200120Concrete exceptions
121-------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000122
Georg Brandlfbd1b222009-12-29 21:38:35 +0000123The following exceptions are the exceptions that are usually raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125.. exception:: AssertionError
126
127 .. index:: statement: assert
128
129 Raised when an :keyword:`assert` statement fails.
130
131
132.. exception:: AttributeError
133
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000134 Raised when an attribute reference (see :ref:`attribute-references`) or
135 assignment fails. (When an object does not support attribute references or
136 attribute assignments at all, :exc:`TypeError` is raised.)
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138
139.. exception:: EOFError
140
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000141 Raised when one of the built-in functions (:func:`input` or :func:`raw_input`)
142 hits an end-of-file condition (EOF) without reading any data. (N.B.: the
Georg Brandl81ac1ce2007-08-31 17:17:17 +0000143 :meth:`file.read` and :meth:`file.readline` methods return an empty string
144 when they hit EOF.)
Georg Brandl116aa622007-08-15 14:28:22 +0000145
146
147.. exception:: FloatingPointError
148
149 Raised when a floating point operation fails. This exception is always defined,
150 but can only be raised when Python is configured with the
Éric Araujo713d3032010-11-18 16:38:46 +0000151 ``--with-fpectl`` option, or the :const:`WANT_SIGFPE_HANDLER` symbol is
Georg Brandl116aa622007-08-15 14:28:22 +0000152 defined in the :file:`pyconfig.h` file.
153
154
155.. exception:: GeneratorExit
156
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000157 Raise when a :term:`generator`\'s :meth:`close` method is called. It
158 directly inherits from :exc:`BaseException` instead of :exc:`Exception` since
159 it is technically not an error.
Georg Brandl116aa622007-08-15 14:28:22 +0000160
Georg Brandl116aa622007-08-15 14:28:22 +0000161
Georg Brandl116aa622007-08-15 14:28:22 +0000162.. exception:: ImportError
163
164 Raised when an :keyword:`import` statement fails to find the module definition
165 or when a ``from ... import`` fails to find a name that is to be imported.
166
Brett Cannon79ec55e2012-04-12 20:24:54 -0400167 The :attr:`name` and :attr:`path` attributes can be set using keyword-only
168 arguments to the constructor. When set they represent the name of the module
169 that was attempted to be imported and the path to any file which triggered
170 the exception, respectively.
171
172 .. versionchanged:: 3.3
173 Added the :attr:`name` and :attr:`path` attributes.
174
Georg Brandl116aa622007-08-15 14:28:22 +0000175
176.. exception:: IndexError
177
Georg Brandl95817b32008-05-11 14:30:18 +0000178 Raised when a sequence subscript is out of range. (Slice indices are
179 silently truncated to fall in the allowed range; if an index is not an
180 integer, :exc:`TypeError` is raised.)
Georg Brandl116aa622007-08-15 14:28:22 +0000181
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000182 .. XXX xref to sequences
Georg Brandl116aa622007-08-15 14:28:22 +0000183
184
185.. exception:: KeyError
186
187 Raised when a mapping (dictionary) key is not found in the set of existing keys.
188
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000189 .. XXX xref to mapping objects?
Georg Brandl116aa622007-08-15 14:28:22 +0000190
191
192.. exception:: KeyboardInterrupt
193
194 Raised when the user hits the interrupt key (normally :kbd:`Control-C` or
Georg Brandl81ac1ce2007-08-31 17:17:17 +0000195 :kbd:`Delete`). During execution, a check for interrupts is made
196 regularly. The exception inherits from :exc:`BaseException` so as to not be
197 accidentally caught by code that catches :exc:`Exception` and thus prevent
198 the interpreter from exiting.
Georg Brandl116aa622007-08-15 14:28:22 +0000199
Georg Brandl116aa622007-08-15 14:28:22 +0000200
201.. exception:: MemoryError
202
203 Raised when an operation runs out of memory but the situation may still be
204 rescued (by deleting some objects). The associated value is a string indicating
205 what kind of (internal) operation ran out of memory. Note that because of the
Georg Brandl60203b42010-10-06 10:11:56 +0000206 underlying memory management architecture (C's :c:func:`malloc` function), the
Georg Brandl116aa622007-08-15 14:28:22 +0000207 interpreter may not always be able to completely recover from this situation; it
208 nevertheless raises an exception so that a stack traceback can be printed, in
209 case a run-away program was the cause.
210
211
212.. exception:: NameError
213
214 Raised when a local or global name is not found. This applies only to
215 unqualified names. The associated value is an error message that includes the
216 name that could not be found.
217
218
219.. exception:: NotImplementedError
220
221 This exception is derived from :exc:`RuntimeError`. In user defined base
222 classes, abstract methods should raise this exception when they require derived
223 classes to override the method.
224
Georg Brandl116aa622007-08-15 14:28:22 +0000225
226.. exception:: OSError
227
Christian Heimesa62da1d2008-01-12 19:39:10 +0000228 .. index:: module: errno
229
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200230 This exception is raised when a system function returns a system-related
231 error, including I/O failures such as "file not found" or "disk full"
232 (not for illegal argument types or other incidental errors). Often a
233 subclass of :exc:`OSError` will actually be raised as described in
234 `OS exceptions`_ below. The :attr:`errno` attribute is a numeric error
235 code from the C variable :c:data:`errno`.
Christian Heimesa62da1d2008-01-12 19:39:10 +0000236
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200237 Under Windows, the :attr:`winerror` attribute gives you the native
238 Windows error code. The :attr:`errno` attribute is then an approximate
239 translation, in POSIX terms, of that native error code.
240
241 Under all platforms, the :attr:`strerror` attribute is the corresponding
242 error message as provided by the operating system (as formatted by the C
243 functions :c:func:`perror` under POSIX, and :c:func:`FormatMessage`
244 Windows).
245
246 For exceptions that involve a file system path (such as :func:`open` or
247 :func:`os.unlink`), the exception instance will contain an additional
248 attribute, :attr:`filename`, which is the file name passed to the function.
Georg Brandl116aa622007-08-15 14:28:22 +0000249
Antoine Pitrou195e7022011-10-12 16:46:46 +0200250 .. versionchanged:: 3.3
251 :exc:`EnvironmentError`, :exc:`IOError`, :exc:`WindowsError`,
252 :exc:`VMSError`, :exc:`socket.error`, :exc:`select.error` and
253 :exc:`mmap.error` have been merged into :exc:`OSError`.
254
Georg Brandl116aa622007-08-15 14:28:22 +0000255
256.. exception:: OverflowError
257
258 Raised when the result of an arithmetic operation is too large to be
Georg Brandlba956ae2007-11-29 17:24:34 +0000259 represented. This cannot occur for integers (which would rather raise
Georg Brandl116aa622007-08-15 14:28:22 +0000260 :exc:`MemoryError` than give up). Because of the lack of standardization of
261 floating point exception handling in C, most floating point operations also
Georg Brandl81ac1ce2007-08-31 17:17:17 +0000262 aren't checked.
Georg Brandl116aa622007-08-15 14:28:22 +0000263
264
265.. exception:: ReferenceError
266
267 This exception is raised when a weak reference proxy, created by the
268 :func:`weakref.proxy` function, is used to access an attribute of the referent
269 after it has been garbage collected. For more information on weak references,
270 see the :mod:`weakref` module.
271
Georg Brandl116aa622007-08-15 14:28:22 +0000272
273.. exception:: RuntimeError
274
275 Raised when an error is detected that doesn't fall in any of the other
276 categories. The associated value is a string indicating what precisely went
277 wrong. (This exception is mostly a relic from a previous version of the
278 interpreter; it is not used very much any more.)
279
280
281.. exception:: StopIteration
282
Georg Brandlc4a55fc2010-02-06 18:46:57 +0000283 Raised by built-in function :func:`next` and an :term:`iterator`\'s
Ezio Melotti1dd7c302012-10-12 13:45:38 +0300284 :meth:`~iterator.__next__` method to signal that there are no further
285 items produced by the iterator.
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000286
287 The exception object has a single attribute :attr:`value`, which is
288 given as an argument when constructing the exception, and defaults
289 to :const:`None`.
290
291 When a generator function returns, a new :exc:`StopIteration` instance is
292 raised, and the value returned by the function is used as the
293 :attr:`value` parameter to the constructor of the exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000294
Nick Coghlan0ed80192012-01-14 14:43:24 +1000295 .. versionchanged:: 3.3
296 Added ``value`` attribute and the ability for generator functions to
297 use it to return a value.
Georg Brandl116aa622007-08-15 14:28:22 +0000298
299.. exception:: SyntaxError
300
301 Raised when the parser encounters a syntax error. This may occur in an
302 :keyword:`import` statement, in a call to the built-in functions :func:`exec`
303 or :func:`eval`, or when reading the initial script or standard input
304 (also interactively).
305
Georg Brandl116aa622007-08-15 14:28:22 +0000306 Instances of this class have attributes :attr:`filename`, :attr:`lineno`,
307 :attr:`offset` and :attr:`text` for easier access to the details. :func:`str`
308 of the exception instance returns only the message.
309
310
Georg Brandl0bdfbfa2010-12-18 17:51:28 +0000311.. exception:: IndentationError
312
313 Base class for syntax errors related to incorrect indentation. This is a
314 subclass of :exc:`SyntaxError`.
315
316
317.. exception:: TabError
318
319 Raised when indentation contains an inconsistent use of tabs and spaces.
320 This is a subclass of :exc:`IndentationError`.
321
322
Georg Brandl116aa622007-08-15 14:28:22 +0000323.. exception:: SystemError
324
325 Raised when the interpreter finds an internal error, but the situation does not
326 look so serious to cause it to abandon all hope. The associated value is a
327 string indicating what went wrong (in low-level terms).
328
329 You should report this to the author or maintainer of your Python interpreter.
330 Be sure to report the version of the Python interpreter (``sys.version``; it is
331 also printed at the start of an interactive Python session), the exact error
332 message (the exception's associated value) and if possible the source of the
333 program that triggered the error.
334
335
336.. exception:: SystemExit
337
338 This exception is raised by the :func:`sys.exit` function. When it is not
339 handled, the Python interpreter exits; no stack traceback is printed. If the
Georg Brandl95817b32008-05-11 14:30:18 +0000340 associated value is an integer, it specifies the system exit status (passed
Georg Brandl60203b42010-10-06 10:11:56 +0000341 to C's :c:func:`exit` function); if it is ``None``, the exit status is zero;
Georg Brandl95817b32008-05-11 14:30:18 +0000342 if it has another type (such as a string), the object's value is printed and
343 the exit status is one.
Georg Brandl116aa622007-08-15 14:28:22 +0000344
Georg Brandl116aa622007-08-15 14:28:22 +0000345 Instances have an attribute :attr:`code` which is set to the proposed exit
346 status or error message (defaulting to ``None``). Also, this exception derives
347 directly from :exc:`BaseException` and not :exc:`Exception`, since it is not
348 technically an error.
349
350 A call to :func:`sys.exit` is translated into an exception so that clean-up
351 handlers (:keyword:`finally` clauses of :keyword:`try` statements) can be
352 executed, and so that a debugger can execute a script without running the risk
353 of losing control. The :func:`os._exit` function can be used if it is
354 absolutely positively necessary to exit immediately (for example, in the child
355 process after a call to :func:`fork`).
356
357 The exception inherits from :exc:`BaseException` instead of :exc:`Exception` so
358 that it is not accidentally caught by code that catches :exc:`Exception`. This
359 allows the exception to properly propagate up and cause the interpreter to exit.
360
Georg Brandl116aa622007-08-15 14:28:22 +0000361
362.. exception:: TypeError
363
364 Raised when an operation or function is applied to an object of inappropriate
365 type. The associated value is a string giving details about the type mismatch.
366
367
368.. exception:: UnboundLocalError
369
370 Raised when a reference is made to a local variable in a function or method, but
371 no value has been bound to that variable. This is a subclass of
372 :exc:`NameError`.
373
Georg Brandl116aa622007-08-15 14:28:22 +0000374
375.. exception:: UnicodeError
376
377 Raised when a Unicode-related encoding or decoding error occurs. It is a
378 subclass of :exc:`ValueError`.
379
Benjamin Peterson78f7e3a2012-12-02 11:33:06 -0500380 :exc:`UnicodeError` has attributes that describe the encoding or decoding
381 error. For example, ``err.object[err.start:err.end]`` gives the particular
382 invalid input that the codec failed on.
383
384 .. attribute:: encoding
385
386 The name of the encoding that raised the error.
387
388 .. attribute:: reason
389
390 A string describing the specific codec error.
391
392 .. attribute:: object
393
394 The object the codec was attempting to encode or decode.
395
396 .. attribute:: start
397
398 The first index of invalid data in :attr:`object`.
399
400 .. attribute:: end
401
402 The index after the last invalid data in :attr:`object`.
403
Georg Brandl116aa622007-08-15 14:28:22 +0000404
405.. exception:: UnicodeEncodeError
406
407 Raised when a Unicode-related error occurs during encoding. It is a subclass of
408 :exc:`UnicodeError`.
409
Georg Brandl116aa622007-08-15 14:28:22 +0000410
411.. exception:: UnicodeDecodeError
412
413 Raised when a Unicode-related error occurs during decoding. It is a subclass of
414 :exc:`UnicodeError`.
415
Georg Brandl116aa622007-08-15 14:28:22 +0000416
417.. exception:: UnicodeTranslateError
418
419 Raised when a Unicode-related error occurs during translating. It is a subclass
420 of :exc:`UnicodeError`.
421
Georg Brandl116aa622007-08-15 14:28:22 +0000422
423.. exception:: ValueError
424
425 Raised when a built-in operation or function receives an argument that has the
426 right type but an inappropriate value, and the situation is not described by a
427 more precise exception such as :exc:`IndexError`.
428
429
Georg Brandl116aa622007-08-15 14:28:22 +0000430.. exception:: ZeroDivisionError
431
432 Raised when the second argument of a division or modulo operation is zero. The
433 associated value is a string indicating the type of the operands and the
434 operation.
435
Georg Brandlfbd1b222009-12-29 21:38:35 +0000436
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200437The following exceptions are kept for compatibility with previous versions;
438starting from Python 3.3, they are aliases of :exc:`OSError`.
439
440.. exception:: EnvironmentError
441
442.. exception:: IOError
443
444.. exception:: VMSError
445
446 Only available on VMS.
447
448.. exception:: WindowsError
449
450 Only available on Windows.
451
452
453OS exceptions
454^^^^^^^^^^^^^
455
456The following exceptions are subclasses of :exc:`OSError`, they get raised
457depending on the system error code.
458
459.. exception:: BlockingIOError
460
461 Raised when an operation would block on an object (e.g. socket) set
462 for non-blocking operation.
463 Corresponds to :c:data:`errno` ``EAGAIN``, ``EALREADY``,
464 ``EWOULDBLOCK`` and ``EINPROGRESS``.
465
Antoine Pitrouf55011f2011-10-12 18:57:23 +0200466 In addition to those of :exc:`OSError`, :exc:`BlockingIOError` can have
467 one more attribute:
468
469 .. attribute:: characters_written
470
471 An integer containing the number of characters written to the stream
472 before it blocked. This attribute is available when using the
473 buffered I/O classes from the :mod:`io` module.
474
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200475.. exception:: ChildProcessError
476
477 Raised when an operation on a child process failed.
478 Corresponds to :c:data:`errno` ``ECHILD``.
479
480.. exception:: ConnectionError
481
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300482 A base class for connection-related issues.
483
484 Subclasses are :exc:`BrokenPipeError`, :exc:`ConnectionAbortedError`,
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200485 :exc:`ConnectionRefusedError` and :exc:`ConnectionResetError`.
486
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300487.. exception:: BrokenPipeError
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200488
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300489 A subclass of :exc:`ConnectionError`, raised when trying to write on a
490 pipe while the other end has been closed, or trying to write on a socket
491 which has been shutdown for writing.
492 Corresponds to :c:data:`errno` ``EPIPE`` and ``ESHUTDOWN``.
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200493
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300494.. exception:: ConnectionAbortedError
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200495
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300496 A subclass of :exc:`ConnectionError`, raised when a connection attempt
497 is aborted by the peer.
498 Corresponds to :c:data:`errno` ``ECONNABORTED``.
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200499
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300500.. exception:: ConnectionRefusedError
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200501
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300502 A subclass of :exc:`ConnectionError`, raised when a connection attempt
503 is refused by the peer.
504 Corresponds to :c:data:`errno` ``ECONNREFUSED``.
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200505
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300506.. exception:: ConnectionResetError
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200507
Ezio Melottib24d3cf2012-10-21 03:22:05 +0300508 A subclass of :exc:`ConnectionError`, raised when a connection is
509 reset by the peer.
510 Corresponds to :c:data:`errno` ``ECONNRESET``.
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200511
512.. exception:: FileExistsError
513
514 Raised when trying to create a file or directory which already exists.
515 Corresponds to :c:data:`errno` ``EEXIST``.
516
517.. exception:: FileNotFoundError
518
519 Raised when a file or directory is requested but doesn't exist.
520 Corresponds to :c:data:`errno` ``ENOENT``.
521
522.. exception:: InterruptedError
523
524 Raised when a system call is interrupted by an incoming signal.
Andrew Svetlov73a5a122012-12-05 11:12:14 +0200525 Corresponds to :c:data:`errno` ``EINTR``.
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200526
527.. exception:: IsADirectoryError
528
529 Raised when a file operation (such as :func:`os.remove`) is requested
530 on a directory.
531 Corresponds to :c:data:`errno` ``EISDIR``.
532
533.. exception:: NotADirectoryError
534
535 Raised when a directory operation (such as :func:`os.listdir`) is requested
536 on something which is not a directory.
537 Corresponds to :c:data:`errno` ``ENOTDIR``.
538
539.. exception:: PermissionError
540
541 Raised when trying to run an operation without the adequate access
542 rights - for example filesystem permissions.
543 Corresponds to :c:data:`errno` ``EACCES`` and ``EPERM``.
544
545.. exception:: ProcessLookupError
546
547 Raised when a given process doesn't exist.
548 Corresponds to :c:data:`errno` ``ESRCH``.
549
550.. exception:: TimeoutError
551
552 Raised when a system function timed out at the system level.
553 Corresponds to :c:data:`errno` ``ETIMEDOUT``.
554
555.. versionadded:: 3.3
556 All the above :exc:`OSError` subclasses were added.
557
558
559.. seealso::
560
561 :pep:`3151` - Reworking the OS and IO exception hierarchy
Antoine Pitrouf9c77462011-10-12 16:02:00 +0200562
563
564Warnings
565--------
566
Georg Brandl116aa622007-08-15 14:28:22 +0000567The following exceptions are used as warning categories; see the :mod:`warnings`
568module for more information.
569
Georg Brandl116aa622007-08-15 14:28:22 +0000570.. exception:: Warning
571
572 Base class for warning categories.
573
574
575.. exception:: UserWarning
576
577 Base class for warnings generated by user code.
578
579
580.. exception:: DeprecationWarning
581
582 Base class for warnings about deprecated features.
583
584
585.. exception:: PendingDeprecationWarning
586
587 Base class for warnings about features which will be deprecated in the future.
588
589
590.. exception:: SyntaxWarning
591
592 Base class for warnings about dubious syntax
593
594
595.. exception:: RuntimeWarning
596
597 Base class for warnings about dubious runtime behavior.
598
599
600.. exception:: FutureWarning
601
602 Base class for warnings about constructs that will change semantically in the
603 future.
604
605
606.. exception:: ImportWarning
607
608 Base class for warnings about probable mistakes in module imports.
609
Georg Brandl116aa622007-08-15 14:28:22 +0000610
611.. exception:: UnicodeWarning
612
613 Base class for warnings related to Unicode.
614
Georg Brandl08be72d2010-10-24 15:11:22 +0000615
Guido van Rossum98297ee2007-11-06 21:34:58 +0000616.. exception:: BytesWarning
Georg Brandl116aa622007-08-15 14:28:22 +0000617
Guido van Rossum98297ee2007-11-06 21:34:58 +0000618 Base class for warnings related to :class:`bytes` and :class:`buffer`.
619
Georg Brandl08be72d2010-10-24 15:11:22 +0000620
621.. exception:: ResourceWarning
622
623 Base class for warnings related to resource usage.
624
625 .. versionadded:: 3.2
626
627
628
Alexandre Vassalottic22c6f22009-07-21 00:51:58 +0000629Exception hierarchy
630-------------------
Guido van Rossum98297ee2007-11-06 21:34:58 +0000631
632The class hierarchy for built-in exceptions is:
Georg Brandl116aa622007-08-15 14:28:22 +0000633
634.. literalinclude:: ../../Lib/test/exception_hierarchy.txt