2007-08-15 11:28:22 -03:00
|
|
|
.. _bltin-exceptions:
|
|
|
|
|
|
|
|
Built-in Exceptions
|
|
|
|
===================
|
|
|
|
|
|
|
|
.. index::
|
2023-05-04 07:48:45 -03:00
|
|
|
pair: statement; try
|
|
|
|
pair: statement; except
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-12-29 17:38:35 -04:00
|
|
|
In Python, all exceptions must be instances of a class that derives from
|
|
|
|
:class:`BaseException`. In a :keyword:`try` statement with an :keyword:`except`
|
2007-08-15 11:28:22 -03:00
|
|
|
clause that mentions a particular class, that clause also handles any exception
|
|
|
|
classes derived from that class (but not exception classes from which *it* is
|
|
|
|
derived). Two exception classes that are not related via subclassing are never
|
|
|
|
equivalent, even if they have the same name.
|
|
|
|
|
2023-05-04 07:48:45 -03:00
|
|
|
.. index:: pair: statement; raise
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2024-02-05 16:22:57 -04:00
|
|
|
The built-in exceptions listed in this chapter can be generated by the interpreter or
|
2007-08-15 11:28:22 -03:00
|
|
|
built-in functions. Except where mentioned, they have an "associated value"
|
2011-01-07 14:28:45 -04:00
|
|
|
indicating the detailed cause of the error. This may be a string or a tuple of
|
|
|
|
several items of information (e.g., an error code and a string explaining the
|
|
|
|
code). The associated value is usually passed as arguments to the exception
|
|
|
|
class's constructor.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
User code can raise built-in exceptions. This can be used to test an exception
|
|
|
|
handler or to report an error condition "just like" the situation in which the
|
|
|
|
interpreter raises the same exception; but beware that there is nothing to
|
|
|
|
prevent user code from raising an inappropriate error.
|
|
|
|
|
2014-04-14 12:20:12 -03:00
|
|
|
The built-in exception classes can be subclassed to define new exceptions;
|
|
|
|
programmers are encouraged to derive new exceptions from the :exc:`Exception`
|
|
|
|
class or one of its subclasses, and not from :exc:`BaseException`. More
|
|
|
|
information on defining exceptions is available in the Python Tutorial under
|
2007-08-15 11:28:22 -03:00
|
|
|
:ref:`tut-userexceptions`.
|
|
|
|
|
2021-10-20 13:54:31 -03:00
|
|
|
|
|
|
|
Exception context
|
|
|
|
-----------------
|
|
|
|
|
2023-12-13 14:59:36 -04:00
|
|
|
.. index:: pair: exception; chaining
|
|
|
|
__cause__ (exception attribute)
|
|
|
|
__context__ (exception attribute)
|
|
|
|
__suppress_context__ (exception attribute)
|
|
|
|
|
|
|
|
Three attributes on exception objects provide information about the context in
|
2023-12-14 10:10:35 -04:00
|
|
|
which the exception was raised:
|
2023-12-13 14:59:36 -04:00
|
|
|
|
|
|
|
.. attribute:: BaseException.__context__
|
|
|
|
BaseException.__cause__
|
|
|
|
BaseException.__suppress_context__
|
|
|
|
|
|
|
|
When raising a new exception while another exception
|
|
|
|
is already being handled, the new exception's
|
|
|
|
:attr:`!__context__` attribute is automatically set to the handled
|
|
|
|
exception. An exception may be handled when an :keyword:`except` or
|
|
|
|
:keyword:`finally` clause, or a :keyword:`with` statement, is used.
|
|
|
|
|
|
|
|
This implicit exception context can be
|
|
|
|
supplemented with an explicit cause by using :keyword:`!from` with
|
|
|
|
:keyword:`raise`::
|
|
|
|
|
|
|
|
raise new_exc from original_exc
|
|
|
|
|
|
|
|
The expression following :keyword:`from<raise>` must be an exception or ``None``. It
|
|
|
|
will be set as :attr:`!__cause__` on the raised exception. Setting
|
|
|
|
:attr:`!__cause__` also implicitly sets the :attr:`!__suppress_context__`
|
|
|
|
attribute to ``True``, so that using ``raise new_exc from None``
|
|
|
|
effectively replaces the old exception with the new one for display
|
|
|
|
purposes (e.g. converting :exc:`KeyError` to :exc:`AttributeError`), while
|
|
|
|
leaving the old exception available in :attr:`!__context__` for introspection
|
|
|
|
when debugging.
|
|
|
|
|
|
|
|
The default traceback display code shows these chained exceptions in
|
|
|
|
addition to the traceback for the exception itself. An explicitly chained
|
|
|
|
exception in :attr:`!__cause__` is always shown when present. An implicitly
|
|
|
|
chained exception in :attr:`!__context__` is shown only if :attr:`!__cause__`
|
|
|
|
is :const:`None` and :attr:`!__suppress_context__` is false.
|
|
|
|
|
|
|
|
In either case, the exception itself is always shown after any chained
|
|
|
|
exceptions so that the final line of the traceback always shows the last
|
|
|
|
exception that was raised.
|
2012-02-26 03:49:52 -04:00
|
|
|
|
2011-10-12 11:02:00 -03:00
|
|
|
|
2021-10-20 13:54:31 -03:00
|
|
|
Inheriting from built-in exceptions
|
|
|
|
-----------------------------------
|
|
|
|
|
|
|
|
User code can create subclasses that inherit from an exception type.
|
|
|
|
It's recommended to only subclass one exception type at a time to avoid
|
|
|
|
any possible conflicts between how the bases handle the ``args``
|
|
|
|
attribute, as well as due to possible memory layout incompatibilities.
|
|
|
|
|
|
|
|
.. impl-detail::
|
|
|
|
|
|
|
|
Most built-in exceptions are implemented in C for efficiency, see:
|
|
|
|
:source:`Objects/exceptions.c`. Some have custom memory layouts
|
|
|
|
which makes it impossible to create a subclass that inherits from
|
|
|
|
multiple exception types. The memory layout of a type is an implementation
|
|
|
|
detail and might change between Python versions, leading to new
|
|
|
|
conflicts in the future. Therefore, it's recommended to avoid
|
|
|
|
subclassing multiple exception types altogether.
|
|
|
|
|
|
|
|
|
2011-10-12 11:02:00 -03:00
|
|
|
Base classes
|
|
|
|
------------
|
|
|
|
|
2009-12-29 17:38:35 -04:00
|
|
|
The following exceptions are used mostly as base classes for other exceptions.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. exception:: BaseException
|
|
|
|
|
|
|
|
The base class for all built-in exceptions. It is not meant to be directly
|
2011-01-07 14:28:45 -04:00
|
|
|
inherited by user-defined classes (for that, use :exc:`Exception`). If
|
2011-11-22 14:34:08 -04:00
|
|
|
:func:`str` is called on an instance of this class, the representation of
|
|
|
|
the argument(s) to the instance are returned, or the empty string when
|
|
|
|
there were no arguments.
|
2011-01-07 14:28:45 -04:00
|
|
|
|
|
|
|
.. attribute:: args
|
|
|
|
|
|
|
|
The tuple of arguments given to the exception constructor. Some built-in
|
2014-03-31 18:44:13 -03:00
|
|
|
exceptions (like :exc:`OSError`) expect a certain number of arguments and
|
2011-01-07 14:28:45 -04:00
|
|
|
assign a special meaning to the elements of this tuple, while others are
|
|
|
|
usually called only with a single string giving an error message.
|
|
|
|
|
|
|
|
.. method:: with_traceback(tb)
|
|
|
|
|
|
|
|
This method sets *tb* as the new traceback for the exception and returns
|
2020-12-16 12:03:32 -04:00
|
|
|
the exception object. It was more commonly used before the exception
|
|
|
|
chaining features of :pep:`3134` became available. The following example
|
|
|
|
shows how we can convert an instance of ``SomeException`` into an
|
|
|
|
instance of ``OtherException`` while preserving the traceback. Once
|
|
|
|
raised, the current frame is pushed onto the traceback of the
|
|
|
|
``OtherException``, as would have happened to the traceback of the
|
2021-05-12 21:11:36 -03:00
|
|
|
original ``SomeException`` had we allowed it to propagate to the caller. ::
|
2011-01-07 14:28:45 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
...
|
|
|
|
except SomeException:
|
2023-02-20 17:54:19 -04:00
|
|
|
tb = sys.exception().__traceback__
|
2011-01-07 14:28:45 -04:00
|
|
|
raise OtherException(...).with_traceback(tb)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-12-13 14:59:36 -04:00
|
|
|
.. attribute:: __traceback__
|
|
|
|
|
|
|
|
A writable field that holds the
|
|
|
|
:ref:`traceback object <traceback-objects>` associated with this
|
|
|
|
exception. See also: :ref:`raise`.
|
|
|
|
|
2022-04-16 15:59:52 -03:00
|
|
|
.. method:: add_note(note)
|
2021-12-03 18:01:15 -04:00
|
|
|
|
2022-04-16 15:59:52 -03:00
|
|
|
Add the string ``note`` to the exception's notes which appear in the standard
|
|
|
|
traceback after the exception string. A :exc:`TypeError` is raised if ``note``
|
|
|
|
is not a string.
|
2021-12-03 18:01:15 -04:00
|
|
|
|
2022-04-16 15:59:52 -03:00
|
|
|
.. versionadded:: 3.11
|
|
|
|
|
|
|
|
.. attribute:: __notes__
|
|
|
|
|
|
|
|
A list of the notes of this exception, which were added with :meth:`add_note`.
|
|
|
|
This attribute is created when :meth:`add_note` is called.
|
|
|
|
|
|
|
|
.. versionadded:: 3.11
|
2021-12-03 18:01:15 -04:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. exception:: Exception
|
|
|
|
|
|
|
|
All built-in, non-system-exiting exceptions are derived from this class. All
|
|
|
|
user-defined exceptions should also be derived from this class.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: ArithmeticError
|
|
|
|
|
|
|
|
The base class for those built-in exceptions that are raised for various
|
|
|
|
arithmetic errors: :exc:`OverflowError`, :exc:`ZeroDivisionError`,
|
|
|
|
:exc:`FloatingPointError`.
|
|
|
|
|
|
|
|
|
2010-12-18 13:51:28 -04:00
|
|
|
.. exception:: BufferError
|
|
|
|
|
|
|
|
Raised when a :ref:`buffer <bufferobjects>` related operation cannot be
|
|
|
|
performed.
|
|
|
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. exception:: LookupError
|
|
|
|
|
Merged revisions 72506,72525-72526,72551,72558,72616,72654-72655,72689,72745,72750,72802,72812,72822,72824,72826-72827,72833,72876,72890,72923,72946,73026,73042,73045,73047,73065,73068-73069 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r72506 | vinay.sajip | 2009-05-09 07:07:17 -0500 (Sat, 09 May 2009) | 1 line
Issue #5971: StreamHandler.handleError now swallows IOErrors which occur when trying to print a traceback.
........
r72525 | benjamin.peterson | 2009-05-09 20:38:02 -0500 (Sat, 09 May 2009) | 1 line
close file explicitly
........
r72526 | benjamin.peterson | 2009-05-09 21:29:00 -0500 (Sat, 09 May 2009) | 1 line
make sure files are closed using the with statement
........
r72551 | benjamin.peterson | 2009-05-10 09:16:47 -0500 (Sun, 10 May 2009) | 1 line
use isinstance
........
r72558 | benjamin.peterson | 2009-05-10 18:52:09 -0500 (Sun, 10 May 2009) | 1 line
sys.setdefaultencoding() strikes me as a bad example
........
r72616 | benjamin.peterson | 2009-05-13 19:33:10 -0500 (Wed, 13 May 2009) | 1 line
importlib.import_module is better these days
........
r72654 | benjamin.peterson | 2009-05-14 17:37:49 -0500 (Thu, 14 May 2009) | 1 line
prevent refleaks from threads
........
r72655 | benjamin.peterson | 2009-05-14 17:40:34 -0500 (Thu, 14 May 2009) | 1 line
a useful decorator for cleaning up threads
........
r72689 | benjamin.peterson | 2009-05-16 13:44:34 -0500 (Sat, 16 May 2009) | 1 line
use skipTest()
........
r72745 | benjamin.peterson | 2009-05-17 09:16:29 -0500 (Sun, 17 May 2009) | 1 line
ignore .rst files in sphinx its self
........
r72750 | benjamin.peterson | 2009-05-17 11:59:27 -0500 (Sun, 17 May 2009) | 1 line
chop off slash
........
r72802 | georg.brandl | 2009-05-20 13:35:27 -0500 (Wed, 20 May 2009) | 1 line
#6051: refer to email examples for better way to construct email messages.
........
r72812 | michael.foord | 2009-05-21 17:57:02 -0500 (Thu, 21 May 2009) | 1 line
Rename TestCase._result to _resultForDoCleanups to avoid potential clashes in TestCase subclasses. Issue 6072.
........
r72822 | georg.brandl | 2009-05-22 04:33:25 -0500 (Fri, 22 May 2009) | 1 line
#6084: fix example.
........
r72824 | georg.brandl | 2009-05-22 04:43:17 -0500 (Fri, 22 May 2009) | 1 line
Fix references to file-related functions and methods (os.* vs file.*).
........
r72826 | georg.brandl | 2009-05-22 04:49:42 -0500 (Fri, 22 May 2009) | 1 line
Fix confusing wording.
........
r72827 | georg.brandl | 2009-05-22 04:50:30 -0500 (Fri, 22 May 2009) | 1 line
s/use/call/
........
r72833 | georg.brandl | 2009-05-22 12:00:17 -0500 (Fri, 22 May 2009) | 1 line
#6078: _warnings is a builtin module and has no standard init_warnings function.
........
r72876 | benjamin.peterson | 2009-05-23 15:59:09 -0500 (Sat, 23 May 2009) | 1 line
remove mention of old ctypes version
........
r72890 | gregory.p.smith | 2009-05-24 13:00:13 -0500 (Sun, 24 May 2009) | 2 lines
add a versionadded tag for set_tunnel
........
r72923 | michael.foord | 2009-05-25 15:36:56 -0500 (Mon, 25 May 2009) | 1 line
Make assertSequenceEqual error messages less cryptic, particularly for nested sequences.
........
r72946 | ronald.oussoren | 2009-05-26 13:44:48 -0500 (Tue, 26 May 2009) | 2 lines
Fixes issue 6110
........
r73026 | r.david.murray | 2009-05-29 14:30:27 -0500 (Fri, 29 May 2009) | 3 lines
Issue 6141: document that the first item of args is still the
command name even when executable is specified.
........
r73042 | benjamin.peterson | 2009-05-29 22:10:52 -0500 (Fri, 29 May 2009) | 1 line
no fdatasync on macos
........
r73045 | georg.brandl | 2009-05-30 02:26:04 -0500 (Sat, 30 May 2009) | 1 line
#6146: fix markup bug.
........
r73047 | georg.brandl | 2009-05-30 05:33:23 -0500 (Sat, 30 May 2009) | 1 line
Fix some more small markup problems.
........
r73065 | antoine.pitrou | 2009-05-30 16:39:25 -0500 (Sat, 30 May 2009) | 3 lines
The test for #5330 wasn't correct.
........
r73068 | antoine.pitrou | 2009-05-30 16:45:40 -0500 (Sat, 30 May 2009) | 3 lines
Update ACKS
........
r73069 | benjamin.peterson | 2009-05-30 19:42:42 -0500 (Sat, 30 May 2009) | 1 line
fix signature
........
2009-06-01 19:42:33 -03:00
|
|
|
The base class for the exceptions that are raised when a key or index used on
|
|
|
|
a mapping or sequence is invalid: :exc:`IndexError`, :exc:`KeyError`. This
|
|
|
|
can be raised directly by :func:`codecs.lookup`.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2011-10-12 11:02:00 -03:00
|
|
|
Concrete exceptions
|
|
|
|
-------------------
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-12-29 17:38:35 -04:00
|
|
|
The following exceptions are the exceptions that are usually raised.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. exception:: AssertionError
|
|
|
|
|
2023-05-04 07:48:45 -03:00
|
|
|
.. index:: pair: statement; assert
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Raised when an :keyword:`assert` statement fails.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: AttributeError
|
|
|
|
|
Merged revisions 59605-59624 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59606 | georg.brandl | 2007-12-29 11:57:00 +0100 (Sat, 29 Dec 2007) | 2 lines
Some cleanup in the docs.
........
r59611 | martin.v.loewis | 2007-12-29 19:49:21 +0100 (Sat, 29 Dec 2007) | 2 lines
Bug #1699: Define _BSD_SOURCE only on OpenBSD.
........
r59612 | raymond.hettinger | 2007-12-29 23:09:34 +0100 (Sat, 29 Dec 2007) | 1 line
Simpler documentation for itertools.tee(). Should be backported.
........
r59613 | raymond.hettinger | 2007-12-29 23:16:24 +0100 (Sat, 29 Dec 2007) | 1 line
Improve docs for itertools.groupby(). The use of xrange(0) to create a unique object is less obvious than object().
........
r59620 | christian.heimes | 2007-12-31 15:47:07 +0100 (Mon, 31 Dec 2007) | 3 lines
Added wininst-9.0.exe executable for VS 2008
Integrated bdist_wininst into PCBuild9 directory
........
r59621 | christian.heimes | 2007-12-31 15:51:18 +0100 (Mon, 31 Dec 2007) | 1 line
Moved PCbuild directory to PC/VS7.1
........
r59622 | christian.heimes | 2007-12-31 15:59:26 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot
........
r59623 | christian.heimes | 2007-12-31 16:02:41 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot, part 2
........
r59624 | christian.heimes | 2007-12-31 16:18:55 +0100 (Mon, 31 Dec 2007) | 1 line
Renamed PCBuild9 directory to PCBuild
........
2007-12-31 12:14:33 -04:00
|
|
|
Raised when an attribute reference (see :ref:`attribute-references`) or
|
|
|
|
assignment fails. (When an object does not support attribute references or
|
|
|
|
attribute assignments at all, :exc:`TypeError` is raised.)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2021-04-13 22:36:07 -03:00
|
|
|
The :attr:`name` and :attr:`obj` attributes can be set using keyword-only
|
|
|
|
arguments to the constructor. When set they represent the name of the attribute
|
|
|
|
that was attempted to be accessed and the object that was accessed for said
|
|
|
|
attribute, respectively.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.10
|
|
|
|
Added the :attr:`name` and :attr:`obj` attributes.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. exception:: EOFError
|
|
|
|
|
2013-10-13 17:09:14 -03:00
|
|
|
Raised when the :func:`input` function hits an end-of-file condition (EOF)
|
|
|
|
without reading any data. (N.B.: the :meth:`io.IOBase.read` and
|
|
|
|
:meth:`io.IOBase.readline` methods return an empty string when they hit EOF.)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. exception:: FloatingPointError
|
|
|
|
|
2018-01-06 03:15:34 -04:00
|
|
|
Not currently used.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. exception:: GeneratorExit
|
|
|
|
|
2015-06-24 12:04:15 -03:00
|
|
|
Raised when a :term:`generator` or :term:`coroutine` is closed;
|
|
|
|
see :meth:`generator.close` and :meth:`coroutine.close`. It
|
Merged revisions 59275-59303 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
NOTE: The merge does NOT contain the modified file Python/import.c from
r59288. I can't get it running. Nick, please check in the PEP 366
manually.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
........
r59279 | georg.brandl | 2007-12-02 19:17:50 +0100 (Sun, 02 Dec 2007) | 2 lines
Fix a sentence I missed before. Do not merge to 3k.
........
r59281 | georg.brandl | 2007-12-02 22:58:54 +0100 (Sun, 02 Dec 2007) | 3 lines
Add documentation for PySys_* functions.
Written by Charlie Shepherd for GHOP. Also fixes #1245.
........
r59288 | nick.coghlan | 2007-12-03 13:55:17 +0100 (Mon, 03 Dec 2007) | 1 line
Implement PEP 366
........
r59290 | christian.heimes | 2007-12-03 14:47:29 +0100 (Mon, 03 Dec 2007) | 3 lines
Applied my patch #1455 with some extra fixes for VS 2005
The new msvc9compiler module supports VS 2005 and VS 2008. I've also fixed build_ext to support PCbuild8 and PCbuild9 and backported my fix for xxmodule.c from py3k. The old code msvccompiler is still in place in case somebody likes to build an extension with VS 2003 or earlier.
I've also updated the cygwin compiler module for VS 2005 and VS 2008. It works with VS 2005 but I'm unable to test it with VS 2008. We have to wait for a new version of cygwin.
........
r59291 | christian.heimes | 2007-12-03 14:55:16 +0100 (Mon, 03 Dec 2007) | 1 line
Added comment to Misc/NEWS for r59290
........
r59292 | christian.heimes | 2007-12-03 15:28:04 +0100 (Mon, 03 Dec 2007) | 1 line
I followed MA Lemberg's suggestion and added comments to the late initialization of the type slots.
........
r59293 | facundo.batista | 2007-12-03 17:29:52 +0100 (Mon, 03 Dec 2007) | 3 lines
Speedup and cleaning of __str__. Thanks Mark Dickinson.
........
r59294 | facundo.batista | 2007-12-03 18:55:00 +0100 (Mon, 03 Dec 2007) | 4 lines
Faster _fix function, and some reordering for a more elegant
coding. Thanks Mark Dickinson.
........
r59295 | martin.v.loewis | 2007-12-03 20:20:02 +0100 (Mon, 03 Dec 2007) | 5 lines
Issue #1727780: Support loading pickles of random.Random objects created
on 32-bit systems on 64-bit systems, and vice versa. As a consequence
of the change, Random pickles created by Python 2.6 cannot be loaded
in Python 2.5.
........
r59297 | facundo.batista | 2007-12-03 20:49:54 +0100 (Mon, 03 Dec 2007) | 3 lines
Two small fixes. Issue 1547.
........
r59299 | georg.brandl | 2007-12-03 20:57:02 +0100 (Mon, 03 Dec 2007) | 2 lines
#1548: fix apostroph placement.
........
r59300 | christian.heimes | 2007-12-03 21:01:02 +0100 (Mon, 03 Dec 2007) | 3 lines
Patch #1537 from Chad Austin
Change GeneratorExit's base class from Exception to BaseException
(This time I'm applying the patch to the correct sandbox.)
........
r59302 | georg.brandl | 2007-12-03 21:03:46 +0100 (Mon, 03 Dec 2007) | 3 lines
Add examples to the xmlrpclib docs.
Written for GHOP by Josip Dzolonga.
........
2007-12-03 17:02:03 -04:00
|
|
|
directly inherits from :exc:`BaseException` instead of :exc:`Exception` since
|
|
|
|
it is technically not an error.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. exception:: ImportError
|
|
|
|
|
2016-09-07 19:42:32 -03:00
|
|
|
Raised when the :keyword:`import` statement has troubles trying to
|
|
|
|
load a module. Also raised when the "from list" in ``from ... import``
|
|
|
|
has a name that cannot be found.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-10-01 14:18:19 -03:00
|
|
|
The optional *name* and *path* keyword-only arguments
|
|
|
|
set the corresponding attributes:
|
|
|
|
|
|
|
|
.. attribute:: name
|
|
|
|
|
|
|
|
The name of the module that was attempted to be imported.
|
|
|
|
|
|
|
|
.. attribute:: path
|
|
|
|
|
|
|
|
The path to any file which triggered the exception.
|
2012-04-12 21:24:54 -03:00
|
|
|
|
|
|
|
.. versionchanged:: 3.3
|
|
|
|
Added the :attr:`name` and :attr:`path` attributes.
|
|
|
|
|
2016-09-07 19:42:32 -03:00
|
|
|
.. exception:: ModuleNotFoundError
|
|
|
|
|
|
|
|
A subclass of :exc:`ImportError` which is raised by :keyword:`import`
|
|
|
|
when a module could not be located. It is also raised when ``None``
|
|
|
|
is found in :data:`sys.modules`.
|
|
|
|
|
|
|
|
.. versionadded:: 3.6
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. exception:: IndexError
|
|
|
|
|
2008-05-11 11:30:18 -03:00
|
|
|
Raised when a sequence subscript is out of range. (Slice indices are
|
|
|
|
silently truncated to fall in the allowed range; if an index is not an
|
|
|
|
integer, :exc:`TypeError` is raised.)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
Merged revisions 59605-59624 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59606 | georg.brandl | 2007-12-29 11:57:00 +0100 (Sat, 29 Dec 2007) | 2 lines
Some cleanup in the docs.
........
r59611 | martin.v.loewis | 2007-12-29 19:49:21 +0100 (Sat, 29 Dec 2007) | 2 lines
Bug #1699: Define _BSD_SOURCE only on OpenBSD.
........
r59612 | raymond.hettinger | 2007-12-29 23:09:34 +0100 (Sat, 29 Dec 2007) | 1 line
Simpler documentation for itertools.tee(). Should be backported.
........
r59613 | raymond.hettinger | 2007-12-29 23:16:24 +0100 (Sat, 29 Dec 2007) | 1 line
Improve docs for itertools.groupby(). The use of xrange(0) to create a unique object is less obvious than object().
........
r59620 | christian.heimes | 2007-12-31 15:47:07 +0100 (Mon, 31 Dec 2007) | 3 lines
Added wininst-9.0.exe executable for VS 2008
Integrated bdist_wininst into PCBuild9 directory
........
r59621 | christian.heimes | 2007-12-31 15:51:18 +0100 (Mon, 31 Dec 2007) | 1 line
Moved PCbuild directory to PC/VS7.1
........
r59622 | christian.heimes | 2007-12-31 15:59:26 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot
........
r59623 | christian.heimes | 2007-12-31 16:02:41 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot, part 2
........
r59624 | christian.heimes | 2007-12-31 16:18:55 +0100 (Mon, 31 Dec 2007) | 1 line
Renamed PCBuild9 directory to PCBuild
........
2007-12-31 12:14:33 -04:00
|
|
|
.. XXX xref to sequences
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. exception:: KeyError
|
|
|
|
|
|
|
|
Raised when a mapping (dictionary) key is not found in the set of existing keys.
|
|
|
|
|
Merged revisions 59605-59624 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59606 | georg.brandl | 2007-12-29 11:57:00 +0100 (Sat, 29 Dec 2007) | 2 lines
Some cleanup in the docs.
........
r59611 | martin.v.loewis | 2007-12-29 19:49:21 +0100 (Sat, 29 Dec 2007) | 2 lines
Bug #1699: Define _BSD_SOURCE only on OpenBSD.
........
r59612 | raymond.hettinger | 2007-12-29 23:09:34 +0100 (Sat, 29 Dec 2007) | 1 line
Simpler documentation for itertools.tee(). Should be backported.
........
r59613 | raymond.hettinger | 2007-12-29 23:16:24 +0100 (Sat, 29 Dec 2007) | 1 line
Improve docs for itertools.groupby(). The use of xrange(0) to create a unique object is less obvious than object().
........
r59620 | christian.heimes | 2007-12-31 15:47:07 +0100 (Mon, 31 Dec 2007) | 3 lines
Added wininst-9.0.exe executable for VS 2008
Integrated bdist_wininst into PCBuild9 directory
........
r59621 | christian.heimes | 2007-12-31 15:51:18 +0100 (Mon, 31 Dec 2007) | 1 line
Moved PCbuild directory to PC/VS7.1
........
r59622 | christian.heimes | 2007-12-31 15:59:26 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot
........
r59623 | christian.heimes | 2007-12-31 16:02:41 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot, part 2
........
r59624 | christian.heimes | 2007-12-31 16:18:55 +0100 (Mon, 31 Dec 2007) | 1 line
Renamed PCBuild9 directory to PCBuild
........
2007-12-31 12:14:33 -04:00
|
|
|
.. XXX xref to mapping objects?
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. exception:: KeyboardInterrupt
|
|
|
|
|
|
|
|
Raised when the user hits the interrupt key (normally :kbd:`Control-C` or
|
2007-08-31 14:17:17 -03:00
|
|
|
:kbd:`Delete`). During execution, a check for interrupts is made
|
|
|
|
regularly. The exception inherits from :exc:`BaseException` so as to not be
|
|
|
|
accidentally caught by code that catches :exc:`Exception` and thus prevent
|
|
|
|
the interpreter from exiting.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2022-03-29 18:21:36 -03:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Catching a :exc:`KeyboardInterrupt` requires special consideration.
|
|
|
|
Because it can be raised at unpredictable points, it may, in some
|
|
|
|
circumstances, leave the running program in an inconsistent state. It is
|
|
|
|
generally best to allow :exc:`KeyboardInterrupt` to end the program as
|
|
|
|
quickly as possible or avoid raising it entirely. (See
|
|
|
|
:ref:`handlers-and-exceptions`.)
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. exception:: MemoryError
|
|
|
|
|
|
|
|
Raised when an operation runs out of memory but the situation may still be
|
|
|
|
rescued (by deleting some objects). The associated value is a string indicating
|
|
|
|
what kind of (internal) operation ran out of memory. Note that because of the
|
2010-10-06 07:11:56 -03:00
|
|
|
underlying memory management architecture (C's :c:func:`malloc` function), the
|
2007-08-15 11:28:22 -03:00
|
|
|
interpreter may not always be able to completely recover from this situation; it
|
|
|
|
nevertheless raises an exception so that a stack traceback can be printed, in
|
|
|
|
case a run-away program was the cause.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: NameError
|
|
|
|
|
|
|
|
Raised when a local or global name is not found. This applies only to
|
|
|
|
unqualified names. The associated value is an error message that includes the
|
|
|
|
name that could not be found.
|
|
|
|
|
2021-04-14 11:10:33 -03:00
|
|
|
The :attr:`name` attribute can be set using a keyword-only argument to the
|
|
|
|
constructor. When set it represent the name of the variable that was attempted
|
|
|
|
to be accessed.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.10
|
|
|
|
Added the :attr:`name` attribute.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. exception:: NotImplementedError
|
|
|
|
|
|
|
|
This exception is derived from :exc:`RuntimeError`. In user defined base
|
2016-08-05 19:10:16 -03:00
|
|
|
classes, abstract methods should raise this exception when they require
|
|
|
|
derived classes to override the method, or while the class is being
|
|
|
|
developed to indicate that the real implementation still needs to be added.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2016-08-05 19:10:16 -03:00
|
|
|
.. note::
|
|
|
|
|
2017-05-19 17:37:57 -03:00
|
|
|
It should not be used to indicate that an operator or method is not
|
2016-08-05 19:10:16 -03:00
|
|
|
meant to be supported at all -- in that case either leave the operator /
|
|
|
|
method undefined or, if a subclass, set it to :data:`None`.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2024-02-29 16:46:12 -04:00
|
|
|
``NotImplementedError`` and :data:`NotImplemented` are not interchangeable,
|
2016-08-05 19:10:16 -03:00
|
|
|
even though they have similar names and purposes. See
|
2024-02-29 16:46:12 -04:00
|
|
|
:data:`!NotImplemented` for details on when to use it.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2015-10-26 08:05:42 -03:00
|
|
|
.. exception:: OSError([arg])
|
|
|
|
OSError(errno, strerror[, filename[, winerror[, filename2]]])
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-05-04 05:17:12 -03:00
|
|
|
.. index:: pair: module; errno
|
Merged revisions 59921-59932 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59923 | raymond.hettinger | 2008-01-11 19:04:55 +0100 (Fri, 11 Jan 2008) | 1 line
Speed-up and simplify code urlparse's result objects.
........
r59924 | andrew.kuchling | 2008-01-11 20:33:24 +0100 (Fri, 11 Jan 2008) | 1 line
Bug #1790: update link; remove outdated paragraph
........
r59925 | thomas.heller | 2008-01-11 20:34:06 +0100 (Fri, 11 Jan 2008) | 5 lines
Raise an error instead of crashing with a segfault when a NULL
function pointer is called.
Will backport to release25-maint.
........
r59927 | thomas.heller | 2008-01-11 21:29:19 +0100 (Fri, 11 Jan 2008) | 4 lines
Fix a potential 'SystemError: NULL result without error'.
NULL may be a valid return value from PyLong_AsVoidPtr.
Will backport to release25-maint.
........
r59928 | raymond.hettinger | 2008-01-12 00:25:18 +0100 (Sat, 12 Jan 2008) | 1 line
Update the opcode docs for STORE_MAP and BUILD_MAP
........
r59929 | mark.dickinson | 2008-01-12 02:56:00 +0100 (Sat, 12 Jan 2008) | 4 lines
Issue 1780: Allow leading and trailing whitespace in Decimal constructor,
when constructing from a string. Disallow trailing newlines in
Context.create_decimal.
........
r59930 | georg.brandl | 2008-01-12 11:53:29 +0100 (Sat, 12 Jan 2008) | 3 lines
Move OSError docs to exceptions doc, remove obsolete descriptions
from os docs, rework posix docs.
........
r59931 | georg.brandl | 2008-01-12 14:47:57 +0100 (Sat, 12 Jan 2008) | 3 lines
Patch #1700288: Method cache optimization, by Armin Rigo, ported to
2.6 by Kevin Jacobs.
........
r59932 | georg.brandl | 2008-01-12 17:11:09 +0100 (Sat, 12 Jan 2008) | 2 lines
Fix editing glitch.
........
2008-01-12 15:39:10 -04:00
|
|
|
|
2011-10-12 11:02:00 -03:00
|
|
|
This exception is raised when a system function returns a system-related
|
|
|
|
error, including I/O failures such as "file not found" or "disk full"
|
2015-10-26 08:05:42 -03:00
|
|
|
(not for illegal argument types or other incidental errors).
|
2011-10-12 11:02:00 -03:00
|
|
|
|
2015-10-26 08:05:42 -03:00
|
|
|
The second form of the constructor sets the corresponding attributes,
|
|
|
|
described below. The attributes default to :const:`None` if not
|
|
|
|
specified. For backwards compatibility, if three arguments are passed,
|
|
|
|
the :attr:`~BaseException.args` attribute contains only a 2-tuple
|
|
|
|
of the first two constructor arguments.
|
Merged revisions 59921-59932 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59923 | raymond.hettinger | 2008-01-11 19:04:55 +0100 (Fri, 11 Jan 2008) | 1 line
Speed-up and simplify code urlparse's result objects.
........
r59924 | andrew.kuchling | 2008-01-11 20:33:24 +0100 (Fri, 11 Jan 2008) | 1 line
Bug #1790: update link; remove outdated paragraph
........
r59925 | thomas.heller | 2008-01-11 20:34:06 +0100 (Fri, 11 Jan 2008) | 5 lines
Raise an error instead of crashing with a segfault when a NULL
function pointer is called.
Will backport to release25-maint.
........
r59927 | thomas.heller | 2008-01-11 21:29:19 +0100 (Fri, 11 Jan 2008) | 4 lines
Fix a potential 'SystemError: NULL result without error'.
NULL may be a valid return value from PyLong_AsVoidPtr.
Will backport to release25-maint.
........
r59928 | raymond.hettinger | 2008-01-12 00:25:18 +0100 (Sat, 12 Jan 2008) | 1 line
Update the opcode docs for STORE_MAP and BUILD_MAP
........
r59929 | mark.dickinson | 2008-01-12 02:56:00 +0100 (Sat, 12 Jan 2008) | 4 lines
Issue 1780: Allow leading and trailing whitespace in Decimal constructor,
when constructing from a string. Disallow trailing newlines in
Context.create_decimal.
........
r59930 | georg.brandl | 2008-01-12 11:53:29 +0100 (Sat, 12 Jan 2008) | 3 lines
Move OSError docs to exceptions doc, remove obsolete descriptions
from os docs, rework posix docs.
........
r59931 | georg.brandl | 2008-01-12 14:47:57 +0100 (Sat, 12 Jan 2008) | 3 lines
Patch #1700288: Method cache optimization, by Armin Rigo, ported to
2.6 by Kevin Jacobs.
........
r59932 | georg.brandl | 2008-01-12 17:11:09 +0100 (Sat, 12 Jan 2008) | 2 lines
Fix editing glitch.
........
2008-01-12 15:39:10 -04:00
|
|
|
|
2015-10-26 08:05:42 -03:00
|
|
|
The constructor often actually returns a subclass of :exc:`OSError`, as
|
|
|
|
described in `OS exceptions`_ below. The particular subclass depends on
|
|
|
|
the final :attr:`.errno` value. This behaviour only occurs when
|
|
|
|
constructing :exc:`OSError` directly or via an alias, and is not
|
|
|
|
inherited when subclassing.
|
2011-10-12 11:02:00 -03:00
|
|
|
|
2015-10-26 08:05:42 -03:00
|
|
|
.. attribute:: errno
|
|
|
|
|
|
|
|
A numeric error code from the C variable :c:data:`errno`.
|
|
|
|
|
|
|
|
.. attribute:: winerror
|
|
|
|
|
|
|
|
Under Windows, this gives you the native
|
|
|
|
Windows error code. The :attr:`.errno` attribute is then an approximate
|
|
|
|
translation, in POSIX terms, of that native error code.
|
|
|
|
|
|
|
|
Under Windows, if the *winerror* constructor argument is an integer,
|
|
|
|
the :attr:`.errno` attribute is determined from the Windows error code,
|
|
|
|
and the *errno* argument is ignored. On other platforms, the
|
|
|
|
*winerror* argument is ignored, and the :attr:`winerror` attribute
|
|
|
|
does not exist.
|
|
|
|
|
|
|
|
.. attribute:: strerror
|
|
|
|
|
|
|
|
The corresponding error message, as provided by
|
|
|
|
the operating system. It is formatted by the C
|
|
|
|
functions :c:func:`perror` under POSIX, and :c:func:`FormatMessage`
|
|
|
|
under Windows.
|
|
|
|
|
|
|
|
.. attribute:: filename
|
|
|
|
filename2
|
|
|
|
|
|
|
|
For exceptions that involve a file system path (such as :func:`open` or
|
|
|
|
:func:`os.unlink`), :attr:`filename` is the file name passed to the function.
|
|
|
|
For functions that involve two file system paths (such as
|
|
|
|
:func:`os.rename`), :attr:`filename2` corresponds to the second
|
|
|
|
file name passed to the function.
|
2014-02-10 02:05:19 -04:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-10-12 11:46:46 -03:00
|
|
|
.. versionchanged:: 3.3
|
|
|
|
:exc:`EnvironmentError`, :exc:`IOError`, :exc:`WindowsError`,
|
2016-04-11 06:23:04 -03:00
|
|
|
:exc:`socket.error`, :exc:`select.error` and
|
2015-10-26 08:05:42 -03:00
|
|
|
:exc:`mmap.error` have been merged into :exc:`OSError`, and the
|
|
|
|
constructor may return a subclass.
|
2011-10-12 11:46:46 -03:00
|
|
|
|
2012-10-29 22:17:38 -03:00
|
|
|
.. versionchanged:: 3.4
|
|
|
|
The :attr:`filename` attribute is now the original file name passed to
|
|
|
|
the function, instead of the name encoded to or decoded from the
|
2020-11-02 11:49:54 -04:00
|
|
|
:term:`filesystem encoding and error handler`. Also, the *filename2*
|
|
|
|
constructor argument and attribute was added.
|
2012-10-29 22:17:38 -03:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. exception:: OverflowError
|
|
|
|
|
|
|
|
Raised when the result of an arithmetic operation is too large to be
|
2007-11-29 13:24:34 -04:00
|
|
|
represented. This cannot occur for integers (which would rather raise
|
2014-06-16 04:31:00 -03:00
|
|
|
:exc:`MemoryError` than give up). However, for historical reasons,
|
|
|
|
OverflowError is sometimes raised for integers that are outside a required
|
|
|
|
range. Because of the lack of standardization of floating point exception
|
|
|
|
handling in C, most floating point operations are not checked.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2024-02-14 18:35:06 -04:00
|
|
|
.. exception:: PythonFinalizationError
|
|
|
|
|
|
|
|
This exception is derived from :exc:`RuntimeError`. It is raised when
|
|
|
|
an operation is blocked during interpreter shutdown also known as
|
|
|
|
:term:`Python finalization <interpreter shutdown>`.
|
|
|
|
|
|
|
|
Examples of operations which can be blocked with a
|
|
|
|
:exc:`PythonFinalizationError` during the Python finalization:
|
|
|
|
|
|
|
|
* Creating a new Python thread.
|
|
|
|
* :func:`os.fork`.
|
|
|
|
|
|
|
|
See also the :func:`sys.is_finalizing` function.
|
|
|
|
|
|
|
|
.. versionadded:: 3.13
|
|
|
|
Previously, a plain :exc:`RuntimeError` was raised.
|
|
|
|
|
|
|
|
|
2015-07-03 02:04:23 -03:00
|
|
|
.. exception:: RecursionError
|
|
|
|
|
|
|
|
This exception is derived from :exc:`RuntimeError`. It is raised when the
|
|
|
|
interpreter detects that the maximum recursion depth (see
|
|
|
|
:func:`sys.getrecursionlimit`) is exceeded.
|
|
|
|
|
|
|
|
.. versionadded:: 3.5
|
|
|
|
Previously, a plain :exc:`RuntimeError` was raised.
|
|
|
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. exception:: ReferenceError
|
|
|
|
|
|
|
|
This exception is raised when a weak reference proxy, created by the
|
|
|
|
:func:`weakref.proxy` function, is used to access an attribute of the referent
|
|
|
|
after it has been garbage collected. For more information on weak references,
|
|
|
|
see the :mod:`weakref` module.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: RuntimeError
|
|
|
|
|
|
|
|
Raised when an error is detected that doesn't fall in any of the other
|
|
|
|
categories. The associated value is a string indicating what precisely went
|
2013-11-25 14:08:32 -04:00
|
|
|
wrong.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. exception:: StopIteration
|
|
|
|
|
2010-02-06 14:46:57 -04:00
|
|
|
Raised by built-in function :func:`next` and an :term:`iterator`\'s
|
2012-10-12 07:45:38 -03:00
|
|
|
:meth:`~iterator.__next__` method to signal that there are no further
|
|
|
|
items produced by the iterator.
|
2012-01-13 07:43:40 -04:00
|
|
|
|
2023-12-05 05:59:52 -04:00
|
|
|
.. attribute:: StopIteration.value
|
|
|
|
|
|
|
|
The exception object has a single attribute :attr:`!value`, which is
|
|
|
|
given as an argument when constructing the exception, and defaults
|
|
|
|
to :const:`None`.
|
2012-01-13 07:43:40 -04:00
|
|
|
|
2015-06-24 12:04:15 -03:00
|
|
|
When a :term:`generator` or :term:`coroutine` function
|
|
|
|
returns, a new :exc:`StopIteration` instance is
|
2012-01-13 07:43:40 -04:00
|
|
|
raised, and the value returned by the function is used as the
|
|
|
|
:attr:`value` parameter to the constructor of the exception.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2018-01-26 16:24:24 -04:00
|
|
|
If a generator code directly or indirectly raises :exc:`StopIteration`,
|
|
|
|
it is converted into a :exc:`RuntimeError` (retaining the
|
|
|
|
:exc:`StopIteration` as the new exception's cause).
|
2015-05-09 12:44:30 -03:00
|
|
|
|
2012-01-14 00:43:24 -04:00
|
|
|
.. versionchanged:: 3.3
|
|
|
|
Added ``value`` attribute and the ability for generator functions to
|
|
|
|
use it to return a value.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2015-05-09 12:44:30 -03:00
|
|
|
.. versionchanged:: 3.5
|
2018-01-26 16:24:24 -04:00
|
|
|
Introduced the RuntimeError transformation via
|
|
|
|
``from __future__ import generator_stop``, see :pep:`479`.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.7
|
|
|
|
Enable :pep:`479` for all code by default: a :exc:`StopIteration`
|
|
|
|
error raised in a generator is transformed into a :exc:`RuntimeError`.
|
2015-05-09 12:44:30 -03:00
|
|
|
|
2015-05-21 12:50:30 -03:00
|
|
|
.. exception:: StopAsyncIteration
|
|
|
|
|
2023-06-28 07:43:11 -03:00
|
|
|
Must be raised by :meth:`~object.__anext__` method of an
|
2015-05-21 12:50:30 -03:00
|
|
|
:term:`asynchronous iterator` object to stop the iteration.
|
|
|
|
|
|
|
|
.. versionadded:: 3.5
|
|
|
|
|
2021-06-06 22:42:31 -03:00
|
|
|
.. exception:: SyntaxError(message, details)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Raised when the parser encounters a syntax error. This may occur in an
|
2021-06-06 22:42:31 -03:00
|
|
|
:keyword:`import` statement, in a call to the built-in functions
|
|
|
|
:func:`compile`, :func:`exec`,
|
2007-08-15 11:28:22 -03:00
|
|
|
or :func:`eval`, or when reading the initial script or standard input
|
|
|
|
(also interactively).
|
|
|
|
|
2021-04-02 18:25:31 -03:00
|
|
|
The :func:`str` of the exception instance returns only the error message.
|
2021-06-06 22:42:31 -03:00
|
|
|
Details is a tuple whose members are also available as separate attributes.
|
2021-04-02 18:25:31 -03:00
|
|
|
|
|
|
|
.. attribute:: filename
|
|
|
|
|
|
|
|
The name of the file the syntax error occurred in.
|
|
|
|
|
|
|
|
.. attribute:: lineno
|
|
|
|
|
|
|
|
Which line number in the file the error occurred in. This is
|
|
|
|
1-indexed: the first line in the file has a ``lineno`` of 1.
|
|
|
|
|
|
|
|
.. attribute:: offset
|
|
|
|
|
|
|
|
The column in the line where the error occurred. This is
|
|
|
|
1-indexed: the first character in the line has an ``offset`` of 1.
|
|
|
|
|
|
|
|
.. attribute:: text
|
|
|
|
|
|
|
|
The source code text involved in the error.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
bpo-43914: Highlight invalid ranges in SyntaxErrors (#25525)
To improve the user experience understanding what part of the error messages associated with SyntaxErrors is wrong, we can highlight the whole error range and not only place the caret at the first character. In this way:
>>> foo(x, z for z in range(10), t, w)
File "<stdin>", line 1
foo(x, z for z in range(10), t, w)
^
SyntaxError: Generator expression must be parenthesized
becomes
>>> foo(x, z for z in range(10), t, w)
File "<stdin>", line 1
foo(x, z for z in range(10), t, w)
^^^^^^^^^^^^^^^^^^^^
SyntaxError: Generator expression must be parenthesized
2021-04-23 10:27:05 -03:00
|
|
|
.. attribute:: end_lineno
|
|
|
|
|
|
|
|
Which line number in the file the error occurred ends in. This is
|
|
|
|
1-indexed: the first line in the file has a ``lineno`` of 1.
|
|
|
|
|
|
|
|
.. attribute:: end_offset
|
|
|
|
|
|
|
|
The column in the end line where the error occurred finishes. This is
|
|
|
|
1-indexed: the first character in the line has an ``offset`` of 1.
|
|
|
|
|
2021-06-06 22:42:31 -03:00
|
|
|
For errors in f-string fields, the message is prefixed by "f-string: "
|
|
|
|
and the offsets are offsets in a text constructed from the replacement
|
|
|
|
expression. For example, compiling f'Bad {a b} field' results in this
|
|
|
|
args attribute: ('f-string: ...', ('', 1, 2, '(a b)\n', 1, 5)).
|
|
|
|
|
bpo-43914: Highlight invalid ranges in SyntaxErrors (#25525)
To improve the user experience understanding what part of the error messages associated with SyntaxErrors is wrong, we can highlight the whole error range and not only place the caret at the first character. In this way:
>>> foo(x, z for z in range(10), t, w)
File "<stdin>", line 1
foo(x, z for z in range(10), t, w)
^
SyntaxError: Generator expression must be parenthesized
becomes
>>> foo(x, z for z in range(10), t, w)
File "<stdin>", line 1
foo(x, z for z in range(10), t, w)
^^^^^^^^^^^^^^^^^^^^
SyntaxError: Generator expression must be parenthesized
2021-04-23 10:27:05 -03:00
|
|
|
.. versionchanged:: 3.10
|
|
|
|
Added the :attr:`end_lineno` and :attr:`end_offset` attributes.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2010-12-18 13:51:28 -04:00
|
|
|
.. exception:: IndentationError
|
|
|
|
|
|
|
|
Base class for syntax errors related to incorrect indentation. This is a
|
|
|
|
subclass of :exc:`SyntaxError`.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: TabError
|
|
|
|
|
|
|
|
Raised when indentation contains an inconsistent use of tabs and spaces.
|
|
|
|
This is a subclass of :exc:`IndentationError`.
|
|
|
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. exception:: SystemError
|
|
|
|
|
|
|
|
Raised when the interpreter finds an internal error, but the situation does not
|
|
|
|
look so serious to cause it to abandon all hope. The associated value is a
|
|
|
|
string indicating what went wrong (in low-level terms).
|
|
|
|
|
|
|
|
You should report this to the author or maintainer of your Python interpreter.
|
|
|
|
Be sure to report the version of the Python interpreter (``sys.version``; it is
|
|
|
|
also printed at the start of an interactive Python session), the exact error
|
|
|
|
message (the exception's associated value) and if possible the source of the
|
|
|
|
program that triggered the error.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: SystemExit
|
|
|
|
|
2015-03-10 09:47:15 -03:00
|
|
|
This exception is raised by the :func:`sys.exit` function. It inherits from
|
|
|
|
:exc:`BaseException` instead of :exc:`Exception` so that it is not accidentally
|
|
|
|
caught by code that catches :exc:`Exception`. This allows the exception to
|
|
|
|
properly propagate up and cause the interpreter to exit. When it is not
|
|
|
|
handled, the Python interpreter exits; no stack traceback is printed. The
|
|
|
|
constructor accepts the same optional argument passed to :func:`sys.exit`.
|
|
|
|
If the value is an integer, it specifies the system exit status (passed to
|
|
|
|
C's :c:func:`exit` function); if it is ``None``, the exit status is zero; if
|
|
|
|
it has another type (such as a string), the object's value is printed and
|
2008-05-11 11:30:18 -03:00
|
|
|
the exit status is one.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
A call to :func:`sys.exit` is translated into an exception so that clean-up
|
|
|
|
handlers (:keyword:`finally` clauses of :keyword:`try` statements) can be
|
|
|
|
executed, and so that a debugger can execute a script without running the risk
|
|
|
|
of losing control. The :func:`os._exit` function can be used if it is
|
|
|
|
absolutely positively necessary to exit immediately (for example, in the child
|
2013-10-13 17:09:14 -03:00
|
|
|
process after a call to :func:`os.fork`).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2015-03-10 09:47:15 -03:00
|
|
|
.. attribute:: code
|
|
|
|
|
|
|
|
The exit status or error message that is passed to the constructor.
|
|
|
|
(Defaults to ``None``.)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. exception:: TypeError
|
|
|
|
|
|
|
|
Raised when an operation or function is applied to an object of inappropriate
|
|
|
|
type. The associated value is a string giving details about the type mismatch.
|
|
|
|
|
2016-08-05 19:10:16 -03:00
|
|
|
This exception may be raised by user code to indicate that an attempted
|
|
|
|
operation on an object is not supported, and is not meant to be. If an object
|
|
|
|
is meant to support a given operation but has not yet provided an
|
|
|
|
implementation, :exc:`NotImplementedError` is the proper exception to raise.
|
|
|
|
|
|
|
|
Passing arguments of the wrong type (e.g. passing a :class:`list` when an
|
|
|
|
:class:`int` is expected) should result in a :exc:`TypeError`, but passing
|
|
|
|
arguments with the wrong value (e.g. a number outside expected boundaries)
|
|
|
|
should result in a :exc:`ValueError`.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. exception:: UnboundLocalError
|
|
|
|
|
|
|
|
Raised when a reference is made to a local variable in a function or method, but
|
|
|
|
no value has been bound to that variable. This is a subclass of
|
|
|
|
:exc:`NameError`.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: UnicodeError
|
|
|
|
|
|
|
|
Raised when a Unicode-related encoding or decoding error occurs. It is a
|
|
|
|
subclass of :exc:`ValueError`.
|
|
|
|
|
2012-12-02 12:33:06 -04:00
|
|
|
:exc:`UnicodeError` has attributes that describe the encoding or decoding
|
|
|
|
error. For example, ``err.object[err.start:err.end]`` gives the particular
|
|
|
|
invalid input that the codec failed on.
|
|
|
|
|
|
|
|
.. attribute:: encoding
|
|
|
|
|
|
|
|
The name of the encoding that raised the error.
|
|
|
|
|
|
|
|
.. attribute:: reason
|
|
|
|
|
|
|
|
A string describing the specific codec error.
|
|
|
|
|
|
|
|
.. attribute:: object
|
|
|
|
|
|
|
|
The object the codec was attempting to encode or decode.
|
|
|
|
|
|
|
|
.. attribute:: start
|
|
|
|
|
|
|
|
The first index of invalid data in :attr:`object`.
|
|
|
|
|
|
|
|
.. attribute:: end
|
|
|
|
|
|
|
|
The index after the last invalid data in :attr:`object`.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. exception:: UnicodeEncodeError
|
|
|
|
|
|
|
|
Raised when a Unicode-related error occurs during encoding. It is a subclass of
|
|
|
|
:exc:`UnicodeError`.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: UnicodeDecodeError
|
|
|
|
|
|
|
|
Raised when a Unicode-related error occurs during decoding. It is a subclass of
|
|
|
|
:exc:`UnicodeError`.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: UnicodeTranslateError
|
|
|
|
|
|
|
|
Raised when a Unicode-related error occurs during translating. It is a subclass
|
|
|
|
of :exc:`UnicodeError`.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: ValueError
|
|
|
|
|
2018-07-17 12:35:26 -03:00
|
|
|
Raised when an operation or function receives an argument that has the
|
2007-08-15 11:28:22 -03:00
|
|
|
right type but an inappropriate value, and the situation is not described by a
|
|
|
|
more precise exception such as :exc:`IndexError`.
|
|
|
|
|
|
|
|
|
2011-10-12 11:02:00 -03:00
|
|
|
.. exception:: ZeroDivisionError
|
Merged revisions 69576,69579-69580,69589,69619-69620,69633,69703-69704,69728-69730 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r69576 | georg.brandl | 2009-02-13 04:56:50 -0600 (Fri, 13 Feb 2009) | 1 line
#1661108: note that urlsafe encoded string can contain "=".
........
r69579 | georg.brandl | 2009-02-13 05:06:59 -0600 (Fri, 13 Feb 2009) | 2 lines
Fix warnings GCC emits where the argument of PyErr_Format is a single variable.
........
r69580 | georg.brandl | 2009-02-13 05:10:04 -0600 (Fri, 13 Feb 2009) | 2 lines
Fix warnings GCC emits where the argument of PyErr_Format is a single variable.
........
r69589 | martin.v.loewis | 2009-02-13 14:11:34 -0600 (Fri, 13 Feb 2009) | 2 lines
Move amd64 properties further to the top, so that they override
the linker options correctly.
........
r69619 | benjamin.peterson | 2009-02-14 11:00:51 -0600 (Sat, 14 Feb 2009) | 1 line
this needn't be a shebang line
........
r69620 | georg.brandl | 2009-02-14 11:01:36 -0600 (Sat, 14 Feb 2009) | 1 line
#5179: don't leak PIPE fds when child execution fails.
........
r69633 | hirokazu.yamamoto | 2009-02-15 03:19:48 -0600 (Sun, 15 Feb 2009) | 1 line
Fixed typo.
........
r69703 | raymond.hettinger | 2009-02-16 16:42:54 -0600 (Mon, 16 Feb 2009) | 3 lines
Issue 5229: Documentation for super() neglects to say what super() actually does
........
r69704 | raymond.hettinger | 2009-02-16 17:00:25 -0600 (Mon, 16 Feb 2009) | 1 line
Add explanation for super(type1, type2).
........
r69728 | georg.brandl | 2009-02-17 18:22:55 -0600 (Tue, 17 Feb 2009) | 2 lines
#5297: fix example.
........
r69729 | georg.brandl | 2009-02-17 18:25:13 -0600 (Tue, 17 Feb 2009) | 2 lines
#5296: sequence -> iterable.
........
r69730 | georg.brandl | 2009-02-17 18:31:36 -0600 (Tue, 17 Feb 2009) | 2 lines
#5268: mention VMSError.
........
2009-02-19 00:22:03 -04:00
|
|
|
|
2011-10-12 11:02:00 -03:00
|
|
|
Raised when the second argument of a division or modulo operation is zero. The
|
|
|
|
associated value is a string indicating the type of the operands and the
|
|
|
|
operation.
|
|
|
|
|
|
|
|
|
|
|
|
The following exceptions are kept for compatibility with previous versions;
|
|
|
|
starting from Python 3.3, they are aliases of :exc:`OSError`.
|
|
|
|
|
|
|
|
.. exception:: EnvironmentError
|
|
|
|
|
|
|
|
.. exception:: IOError
|
Merged revisions 69576,69579-69580,69589,69619-69620,69633,69703-69704,69728-69730 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r69576 | georg.brandl | 2009-02-13 04:56:50 -0600 (Fri, 13 Feb 2009) | 1 line
#1661108: note that urlsafe encoded string can contain "=".
........
r69579 | georg.brandl | 2009-02-13 05:06:59 -0600 (Fri, 13 Feb 2009) | 2 lines
Fix warnings GCC emits where the argument of PyErr_Format is a single variable.
........
r69580 | georg.brandl | 2009-02-13 05:10:04 -0600 (Fri, 13 Feb 2009) | 2 lines
Fix warnings GCC emits where the argument of PyErr_Format is a single variable.
........
r69589 | martin.v.loewis | 2009-02-13 14:11:34 -0600 (Fri, 13 Feb 2009) | 2 lines
Move amd64 properties further to the top, so that they override
the linker options correctly.
........
r69619 | benjamin.peterson | 2009-02-14 11:00:51 -0600 (Sat, 14 Feb 2009) | 1 line
this needn't be a shebang line
........
r69620 | georg.brandl | 2009-02-14 11:01:36 -0600 (Sat, 14 Feb 2009) | 1 line
#5179: don't leak PIPE fds when child execution fails.
........
r69633 | hirokazu.yamamoto | 2009-02-15 03:19:48 -0600 (Sun, 15 Feb 2009) | 1 line
Fixed typo.
........
r69703 | raymond.hettinger | 2009-02-16 16:42:54 -0600 (Mon, 16 Feb 2009) | 3 lines
Issue 5229: Documentation for super() neglects to say what super() actually does
........
r69704 | raymond.hettinger | 2009-02-16 17:00:25 -0600 (Mon, 16 Feb 2009) | 1 line
Add explanation for super(type1, type2).
........
r69728 | georg.brandl | 2009-02-17 18:22:55 -0600 (Tue, 17 Feb 2009) | 2 lines
#5297: fix example.
........
r69729 | georg.brandl | 2009-02-17 18:25:13 -0600 (Tue, 17 Feb 2009) | 2 lines
#5296: sequence -> iterable.
........
r69730 | georg.brandl | 2009-02-17 18:31:36 -0600 (Tue, 17 Feb 2009) | 2 lines
#5268: mention VMSError.
........
2009-02-19 00:22:03 -04:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. exception:: WindowsError
|
|
|
|
|
2011-10-12 11:02:00 -03:00
|
|
|
Only available on Windows.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2011-10-12 11:02:00 -03:00
|
|
|
OS exceptions
|
|
|
|
^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
The following exceptions are subclasses of :exc:`OSError`, they get raised
|
|
|
|
depending on the system error code.
|
|
|
|
|
|
|
|
.. exception:: BlockingIOError
|
|
|
|
|
|
|
|
Raised when an operation would block on an object (e.g. socket) set
|
|
|
|
for non-blocking operation.
|
2023-07-21 06:40:37 -03:00
|
|
|
Corresponds to :c:data:`errno` :py:const:`~errno.EAGAIN`, :py:const:`~errno.EALREADY`,
|
|
|
|
:py:const:`~errno.EWOULDBLOCK` and :py:const:`~errno.EINPROGRESS`.
|
2011-10-12 11:02:00 -03:00
|
|
|
|
2011-10-12 13:57:23 -03:00
|
|
|
In addition to those of :exc:`OSError`, :exc:`BlockingIOError` can have
|
|
|
|
one more attribute:
|
|
|
|
|
|
|
|
.. attribute:: characters_written
|
|
|
|
|
|
|
|
An integer containing the number of characters written to the stream
|
|
|
|
before it blocked. This attribute is available when using the
|
|
|
|
buffered I/O classes from the :mod:`io` module.
|
|
|
|
|
2011-10-12 11:02:00 -03:00
|
|
|
.. exception:: ChildProcessError
|
|
|
|
|
|
|
|
Raised when an operation on a child process failed.
|
2023-07-21 06:40:37 -03:00
|
|
|
Corresponds to :c:data:`errno` :py:const:`~errno.ECHILD`.
|
2011-10-12 11:02:00 -03:00
|
|
|
|
|
|
|
.. exception:: ConnectionError
|
|
|
|
|
2012-10-20 21:22:05 -03:00
|
|
|
A base class for connection-related issues.
|
|
|
|
|
|
|
|
Subclasses are :exc:`BrokenPipeError`, :exc:`ConnectionAbortedError`,
|
2011-10-12 11:02:00 -03:00
|
|
|
:exc:`ConnectionRefusedError` and :exc:`ConnectionResetError`.
|
|
|
|
|
2012-10-20 21:22:05 -03:00
|
|
|
.. exception:: BrokenPipeError
|
2011-10-12 11:02:00 -03:00
|
|
|
|
2012-10-20 21:22:05 -03:00
|
|
|
A subclass of :exc:`ConnectionError`, raised when trying to write on a
|
|
|
|
pipe while the other end has been closed, or trying to write on a socket
|
|
|
|
which has been shutdown for writing.
|
2023-07-21 06:40:37 -03:00
|
|
|
Corresponds to :c:data:`errno` :py:const:`~errno.EPIPE` and :py:const:`~errno.ESHUTDOWN`.
|
2011-10-12 11:02:00 -03:00
|
|
|
|
2012-10-20 21:22:05 -03:00
|
|
|
.. exception:: ConnectionAbortedError
|
2011-10-12 11:02:00 -03:00
|
|
|
|
2012-10-20 21:22:05 -03:00
|
|
|
A subclass of :exc:`ConnectionError`, raised when a connection attempt
|
|
|
|
is aborted by the peer.
|
2023-07-21 06:40:37 -03:00
|
|
|
Corresponds to :c:data:`errno` :py:const:`~errno.ECONNABORTED`.
|
2011-10-12 11:02:00 -03:00
|
|
|
|
2012-10-20 21:22:05 -03:00
|
|
|
.. exception:: ConnectionRefusedError
|
2011-10-12 11:02:00 -03:00
|
|
|
|
2012-10-20 21:22:05 -03:00
|
|
|
A subclass of :exc:`ConnectionError`, raised when a connection attempt
|
|
|
|
is refused by the peer.
|
2023-07-21 06:40:37 -03:00
|
|
|
Corresponds to :c:data:`errno` :py:const:`~errno.ECONNREFUSED`.
|
2011-10-12 11:02:00 -03:00
|
|
|
|
2012-10-20 21:22:05 -03:00
|
|
|
.. exception:: ConnectionResetError
|
2011-10-12 11:02:00 -03:00
|
|
|
|
2012-10-20 21:22:05 -03:00
|
|
|
A subclass of :exc:`ConnectionError`, raised when a connection is
|
|
|
|
reset by the peer.
|
2023-07-21 06:40:37 -03:00
|
|
|
Corresponds to :c:data:`errno` :py:const:`~errno.ECONNRESET`.
|
2011-10-12 11:02:00 -03:00
|
|
|
|
|
|
|
.. exception:: FileExistsError
|
|
|
|
|
|
|
|
Raised when trying to create a file or directory which already exists.
|
2023-07-21 06:40:37 -03:00
|
|
|
Corresponds to :c:data:`errno` :py:const:`~errno.EEXIST`.
|
2011-10-12 11:02:00 -03:00
|
|
|
|
|
|
|
.. exception:: FileNotFoundError
|
|
|
|
|
|
|
|
Raised when a file or directory is requested but doesn't exist.
|
2023-07-21 06:40:37 -03:00
|
|
|
Corresponds to :c:data:`errno` :py:const:`~errno.ENOENT`.
|
2011-10-12 11:02:00 -03:00
|
|
|
|
|
|
|
.. exception:: InterruptedError
|
|
|
|
|
|
|
|
Raised when a system call is interrupted by an incoming signal.
|
2023-07-21 06:40:37 -03:00
|
|
|
Corresponds to :c:data:`errno` :py:const:`~errno.EINTR`.
|
2015-03-30 16:16:11 -03:00
|
|
|
|
|
|
|
.. versionchanged:: 3.5
|
|
|
|
Python now retries system calls when a syscall is interrupted by a
|
|
|
|
signal, except if the signal handler raises an exception (see :pep:`475`
|
|
|
|
for the rationale), instead of raising :exc:`InterruptedError`.
|
2011-10-12 11:02:00 -03:00
|
|
|
|
|
|
|
.. exception:: IsADirectoryError
|
|
|
|
|
|
|
|
Raised when a file operation (such as :func:`os.remove`) is requested
|
|
|
|
on a directory.
|
2023-07-21 06:40:37 -03:00
|
|
|
Corresponds to :c:data:`errno` :py:const:`~errno.EISDIR`.
|
2011-10-12 11:02:00 -03:00
|
|
|
|
|
|
|
.. exception:: NotADirectoryError
|
|
|
|
|
2021-08-03 08:28:09 -03:00
|
|
|
Raised when a directory operation (such as :func:`os.listdir`) is requested on
|
|
|
|
something which is not a directory. On most POSIX platforms, it may also be
|
|
|
|
raised if an operation attempts to open or traverse a non-directory file as if
|
|
|
|
it were a directory.
|
2023-07-21 06:40:37 -03:00
|
|
|
Corresponds to :c:data:`errno` :py:const:`~errno.ENOTDIR`.
|
2011-10-12 11:02:00 -03:00
|
|
|
|
|
|
|
.. exception:: PermissionError
|
|
|
|
|
|
|
|
Raised when trying to run an operation without the adequate access
|
|
|
|
rights - for example filesystem permissions.
|
2023-07-21 06:40:37 -03:00
|
|
|
Corresponds to :c:data:`errno` :py:const:`~errno.EACCES`,
|
|
|
|
:py:const:`~errno.EPERM`, and :py:const:`~errno.ENOTCAPABLE`.
|
2022-08-16 15:20:15 -03:00
|
|
|
|
|
|
|
.. versionchanged:: 3.11.1
|
2023-07-21 06:40:37 -03:00
|
|
|
WASI's :py:const:`~errno.ENOTCAPABLE` is now mapped to
|
2022-08-16 15:20:15 -03:00
|
|
|
:exc:`PermissionError`.
|
2011-10-12 11:02:00 -03:00
|
|
|
|
|
|
|
.. exception:: ProcessLookupError
|
|
|
|
|
|
|
|
Raised when a given process doesn't exist.
|
2023-07-21 06:40:37 -03:00
|
|
|
Corresponds to :c:data:`errno` :py:const:`~errno.ESRCH`.
|
2011-10-12 11:02:00 -03:00
|
|
|
|
|
|
|
.. exception:: TimeoutError
|
|
|
|
|
|
|
|
Raised when a system function timed out at the system level.
|
2023-07-21 06:40:37 -03:00
|
|
|
Corresponds to :c:data:`errno` :py:const:`~errno.ETIMEDOUT`.
|
2011-10-12 11:02:00 -03:00
|
|
|
|
|
|
|
.. versionadded:: 3.3
|
|
|
|
All the above :exc:`OSError` subclasses were added.
|
|
|
|
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:pep:`3151` - Reworking the OS and IO exception hierarchy
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2018-01-07 22:45:02 -04:00
|
|
|
.. _warning-categories-as-exceptions:
|
|
|
|
|
2011-10-12 11:02:00 -03:00
|
|
|
Warnings
|
|
|
|
--------
|
2009-12-29 17:38:35 -04:00
|
|
|
|
2018-01-07 22:45:02 -04:00
|
|
|
The following exceptions are used as warning categories; see the
|
|
|
|
:ref:`warning-categories` documentation for more details.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. exception:: Warning
|
|
|
|
|
|
|
|
Base class for warning categories.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: UserWarning
|
|
|
|
|
|
|
|
Base class for warnings generated by user code.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: DeprecationWarning
|
|
|
|
|
2018-01-07 22:45:02 -04:00
|
|
|
Base class for warnings about deprecated features when those warnings are
|
|
|
|
intended for other Python developers.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2020-01-24 05:22:18 -04:00
|
|
|
Ignored by the default warning filters, except in the ``__main__`` module
|
|
|
|
(:pep:`565`). Enabling the :ref:`Python Development Mode <devmode>` shows
|
|
|
|
this warning.
|
|
|
|
|
2021-09-02 11:44:50 -03:00
|
|
|
The deprecation policy is described in :pep:`387`.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. exception:: PendingDeprecationWarning
|
|
|
|
|
2019-04-15 09:40:23 -03:00
|
|
|
Base class for warnings about features which are obsolete and
|
|
|
|
expected to be deprecated in the future, but are not deprecated
|
|
|
|
at the moment.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2019-04-15 09:40:23 -03:00
|
|
|
This class is rarely used as emitting a warning about a possible
|
|
|
|
upcoming deprecation is unusual, and :exc:`DeprecationWarning`
|
|
|
|
is preferred for already active deprecations.
|
2019-04-05 05:54:24 -03:00
|
|
|
|
2020-01-24 05:22:18 -04:00
|
|
|
Ignored by the default warning filters. Enabling the :ref:`Python
|
|
|
|
Development Mode <devmode>` shows this warning.
|
|
|
|
|
2021-09-02 11:44:50 -03:00
|
|
|
The deprecation policy is described in :pep:`387`.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. exception:: SyntaxWarning
|
|
|
|
|
2015-10-10 07:36:22 -03:00
|
|
|
Base class for warnings about dubious syntax.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. exception:: RuntimeWarning
|
|
|
|
|
|
|
|
Base class for warnings about dubious runtime behavior.
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: FutureWarning
|
|
|
|
|
2018-01-07 22:45:02 -04:00
|
|
|
Base class for warnings about deprecated features when those warnings are
|
|
|
|
intended for end users of applications that are written in Python.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. exception:: ImportWarning
|
|
|
|
|
|
|
|
Base class for warnings about probable mistakes in module imports.
|
|
|
|
|
2020-01-24 05:22:18 -04:00
|
|
|
Ignored by the default warning filters. Enabling the :ref:`Python
|
|
|
|
Development Mode <devmode>` shows this warning.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. exception:: UnicodeWarning
|
|
|
|
|
|
|
|
Base class for warnings related to Unicode.
|
|
|
|
|
2010-10-24 12:11:22 -03:00
|
|
|
|
2021-03-29 00:28:14 -03:00
|
|
|
.. exception:: EncodingWarning
|
|
|
|
|
|
|
|
Base class for warnings related to encodings.
|
|
|
|
|
|
|
|
See :ref:`io-encoding-warning` for details.
|
|
|
|
|
|
|
|
.. versionadded:: 3.10
|
|
|
|
|
|
|
|
|
2007-11-06 17:34:58 -04:00
|
|
|
.. exception:: BytesWarning
|
|
|
|
|
2013-10-13 17:09:14 -03:00
|
|
|
Base class for warnings related to :class:`bytes` and :class:`bytearray`.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2010-10-24 12:11:22 -03:00
|
|
|
|
|
|
|
.. exception:: ResourceWarning
|
|
|
|
|
2020-01-24 05:22:18 -04:00
|
|
|
Base class for warnings related to resource usage.
|
|
|
|
|
|
|
|
Ignored by the default warning filters. Enabling the :ref:`Python
|
|
|
|
Development Mode <devmode>` shows this warning.
|
2010-10-24 12:11:22 -03:00
|
|
|
|
|
|
|
.. versionadded:: 3.2
|
|
|
|
|
|
|
|
|
2023-07-06 02:41:04 -03:00
|
|
|
.. _lib-exception-groups:
|
|
|
|
|
2022-01-06 15:05:34 -04:00
|
|
|
Exception groups
|
|
|
|
----------------
|
|
|
|
|
|
|
|
The following are used when it is necessary to raise multiple unrelated
|
|
|
|
exceptions. They are part of the exception hierarchy so they can be
|
|
|
|
handled with :keyword:`except` like all other exceptions. In addition,
|
|
|
|
they are recognised by :keyword:`except*<except_star>`, which matches
|
|
|
|
their subgroups based on the types of the contained exceptions.
|
|
|
|
|
|
|
|
.. exception:: ExceptionGroup(msg, excs)
|
|
|
|
.. exception:: BaseExceptionGroup(msg, excs)
|
|
|
|
|
|
|
|
Both of these exception types wrap the exceptions in the sequence ``excs``.
|
|
|
|
The ``msg`` parameter must be a string. The difference between the two
|
|
|
|
classes is that :exc:`BaseExceptionGroup` extends :exc:`BaseException` and
|
|
|
|
it can wrap any exception, while :exc:`ExceptionGroup` extends :exc:`Exception`
|
|
|
|
and it can only wrap subclasses of :exc:`Exception`. This design is so that
|
|
|
|
``except Exception`` catches an :exc:`ExceptionGroup` but not
|
|
|
|
:exc:`BaseExceptionGroup`.
|
|
|
|
|
|
|
|
The :exc:`BaseExceptionGroup` constructor returns an :exc:`ExceptionGroup`
|
|
|
|
rather than a :exc:`BaseExceptionGroup` if all contained exceptions are
|
|
|
|
:exc:`Exception` instances, so it can be used to make the selection
|
|
|
|
automatic. The :exc:`ExceptionGroup` constructor, on the other hand,
|
|
|
|
raises a :exc:`TypeError` if any contained exception is not an
|
|
|
|
:exc:`Exception` subclass.
|
|
|
|
|
2022-01-24 17:50:18 -04:00
|
|
|
.. attribute:: message
|
|
|
|
|
|
|
|
The ``msg`` argument to the constructor. This is a read-only attribute.
|
|
|
|
|
|
|
|
.. attribute:: exceptions
|
|
|
|
|
|
|
|
A tuple of the exceptions in the ``excs`` sequence given to the
|
|
|
|
constructor. This is a read-only attribute.
|
|
|
|
|
2022-01-06 15:05:34 -04:00
|
|
|
.. method:: subgroup(condition)
|
|
|
|
|
2022-01-24 17:50:18 -04:00
|
|
|
Returns an exception group that contains only the exceptions from the
|
|
|
|
current group that match *condition*, or ``None`` if the result is empty.
|
2022-01-06 15:05:34 -04:00
|
|
|
|
2023-06-23 15:47:47 -03:00
|
|
|
The condition can be an exception type or tuple of exception types, in which
|
|
|
|
case each exception is checked for a match using the same check that is used
|
|
|
|
in an ``except`` clause. The condition can also be a callable (other than
|
|
|
|
a type object) that accepts an exception as its single argument and returns
|
|
|
|
true for the exceptions that should be in the subgroup.
|
2022-01-06 15:05:34 -04:00
|
|
|
|
2022-01-24 17:50:18 -04:00
|
|
|
The nesting structure of the current exception is preserved in the result,
|
2023-12-13 14:59:36 -04:00
|
|
|
as are the values of its :attr:`message`,
|
|
|
|
:attr:`~BaseException.__traceback__`, :attr:`~BaseException.__cause__`,
|
|
|
|
:attr:`~BaseException.__context__` and
|
|
|
|
:attr:`~BaseException.__notes__` fields.
|
2022-01-24 17:50:18 -04:00
|
|
|
Empty nested groups are omitted from the result.
|
2022-01-06 15:05:34 -04:00
|
|
|
|
2022-01-24 17:50:18 -04:00
|
|
|
The condition is checked for all exceptions in the nested exception group,
|
|
|
|
including the top-level and any nested exception groups. If the condition is
|
|
|
|
true for such an exception group, it is included in the result in full.
|
2022-01-06 15:05:34 -04:00
|
|
|
|
2023-06-23 15:47:47 -03:00
|
|
|
.. versionadded:: 3.13
|
|
|
|
``condition`` can be any callable which is not a type object.
|
|
|
|
|
2022-01-06 15:05:34 -04:00
|
|
|
.. method:: split(condition)
|
|
|
|
|
2022-01-24 17:50:18 -04:00
|
|
|
Like :meth:`subgroup`, but returns the pair ``(match, rest)`` where ``match``
|
|
|
|
is ``subgroup(condition)`` and ``rest`` is the remaining non-matching
|
|
|
|
part.
|
2022-01-06 15:05:34 -04:00
|
|
|
|
|
|
|
.. method:: derive(excs)
|
|
|
|
|
2022-11-23 06:59:52 -04:00
|
|
|
Returns an exception group with the same :attr:`message`, but which
|
|
|
|
wraps the exceptions in ``excs``.
|
2022-01-24 17:50:18 -04:00
|
|
|
|
2024-06-05 08:56:01 -03:00
|
|
|
This method is used by :meth:`subgroup` and :meth:`split`, which
|
|
|
|
are used in various contexts to break up an exception group. A
|
2022-01-24 17:50:18 -04:00
|
|
|
subclass needs to override it in order to make :meth:`subgroup`
|
|
|
|
and :meth:`split` return instances of the subclass rather
|
2022-11-23 06:59:52 -04:00
|
|
|
than :exc:`ExceptionGroup`.
|
|
|
|
|
2023-12-13 14:59:36 -04:00
|
|
|
:meth:`subgroup` and :meth:`split` copy the
|
|
|
|
:attr:`~BaseException.__traceback__`,
|
|
|
|
:attr:`~BaseException.__cause__`, :attr:`~BaseException.__context__` and
|
|
|
|
:attr:`~BaseException.__notes__` fields from
|
2022-11-23 06:59:52 -04:00
|
|
|
the original exception group to the one returned by :meth:`derive`, so
|
2023-12-13 14:59:36 -04:00
|
|
|
these fields do not need to be updated by :meth:`derive`.
|
|
|
|
|
|
|
|
.. doctest::
|
2022-01-24 17:50:18 -04:00
|
|
|
|
|
|
|
>>> class MyGroup(ExceptionGroup):
|
2023-03-22 15:14:05 -03:00
|
|
|
... def derive(self, excs):
|
|
|
|
... return MyGroup(self.message, excs)
|
2022-01-24 17:50:18 -04:00
|
|
|
...
|
2022-11-23 06:59:52 -04:00
|
|
|
>>> e = MyGroup("eg", [ValueError(1), TypeError(2)])
|
|
|
|
>>> e.add_note("a note")
|
|
|
|
>>> e.__context__ = Exception("context")
|
|
|
|
>>> e.__cause__ = Exception("cause")
|
|
|
|
>>> try:
|
|
|
|
... raise e
|
|
|
|
... except Exception as e:
|
|
|
|
... exc = e
|
|
|
|
...
|
|
|
|
>>> match, rest = exc.split(ValueError)
|
|
|
|
>>> exc, exc.__context__, exc.__cause__, exc.__notes__
|
|
|
|
(MyGroup('eg', [ValueError(1), TypeError(2)]), Exception('context'), Exception('cause'), ['a note'])
|
|
|
|
>>> match, match.__context__, match.__cause__, match.__notes__
|
|
|
|
(MyGroup('eg', [ValueError(1)]), Exception('context'), Exception('cause'), ['a note'])
|
|
|
|
>>> rest, rest.__context__, rest.__cause__, rest.__notes__
|
|
|
|
(MyGroup('eg', [TypeError(2)]), Exception('context'), Exception('cause'), ['a note'])
|
|
|
|
>>> exc.__traceback__ is match.__traceback__ is rest.__traceback__
|
|
|
|
True
|
|
|
|
|
2022-01-24 17:50:18 -04:00
|
|
|
|
2023-12-15 13:15:34 -04:00
|
|
|
Note that :exc:`BaseExceptionGroup` defines :meth:`~object.__new__`, so
|
2022-01-24 17:50:18 -04:00
|
|
|
subclasses that need a different constructor signature need to
|
2023-12-15 13:15:34 -04:00
|
|
|
override that rather than :meth:`~object.__init__`. For example, the following
|
2022-01-24 17:50:18 -04:00
|
|
|
defines an exception group subclass which accepts an exit_code and
|
|
|
|
and constructs the group's message from it. ::
|
|
|
|
|
|
|
|
class Errors(ExceptionGroup):
|
|
|
|
def __new__(cls, errors, exit_code):
|
|
|
|
self = super().__new__(Errors, f"exit code: {exit_code}", errors)
|
|
|
|
self.exit_code = exit_code
|
|
|
|
return self
|
|
|
|
|
|
|
|
def derive(self, excs):
|
|
|
|
return Errors(excs, self.exit_code)
|
2022-01-06 15:05:34 -04:00
|
|
|
|
2022-11-18 11:44:43 -04:00
|
|
|
Like :exc:`ExceptionGroup`, any subclass of :exc:`BaseExceptionGroup` which
|
|
|
|
is also a subclass of :exc:`Exception` can only wrap instances of
|
|
|
|
:exc:`Exception`.
|
|
|
|
|
2022-01-06 15:05:34 -04:00
|
|
|
.. versionadded:: 3.11
|
|
|
|
|
2010-10-24 12:11:22 -03:00
|
|
|
|
2009-07-20 21:51:58 -03:00
|
|
|
Exception hierarchy
|
|
|
|
-------------------
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2007-11-06 17:34:58 -04:00
|
|
|
The class hierarchy for built-in exceptions is:
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. literalinclude:: ../../Lib/test/exception_hierarchy.txt
|
2021-06-11 03:53:52 -03:00
|
|
|
:language: text
|