<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>2.2 Built-in Exceptions</TITLE>
<META NAME="description" CONTENT="2.2 Built-in Exceptions">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="next" HREF="built-in-funcs.html">
<LINK REL="previous" HREF="types.html">
<LINK REL="up" HREF="builtin.html">
<LINK REL="next" HREF="built-in-funcs.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="built-in-funcs.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="builtin.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="specialattrs.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="built-in-funcs.html">2.3 Built-in Functions</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="builtin.html">2. Built-in Types, Exceptions</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="specialattrs.html">2.1.8 Special Attributes</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION004200000000000000000">
2.2 Built-in Exceptions</A>
</H1>

<P>


<P>
Exceptions can be class objects or string objects.  While
traditionally, most exceptions have been string objects, in Python
1.5, all standard exceptions have been converted to class objects,
and users are encouraged to do the same.  The source code for those
exceptions is present in the standard library module
<tt class=module>exceptions</tt>; this module never needs to be imported explicitly.

<P>
For backward compatibility, when Python is invoked with the <tt>-X</tt>option, most of the standard exceptions are strings<A NAME="tex2html7"
 HREF="#foot2122"><SUP>2.7</SUP></A>.  This option may be used to
run code that breaks because of the different semantics of class based
exceptions.  The <tt>-X</tt> option will become obsolete in future
Python versions, so the recommended solution is to fix the code.

<P>
Two distinct string objects with the same value are considered different
exceptions.  This is done to force programmers to use exception names
rather than their string value when specifying exception handlers.
The string value of all built-in exceptions is their name, but this is
not a requirement for user-defined exceptions or exceptions defined by
library modules.

<P>
For class exceptions, in a <tt class=keyword>try</tt> statement with an <tt class=keyword>except</tt>
clause that mentions a particular class, that clause also handles
any exception classes derived from that class (but not exception
classes from which <i>it</i> is derived).  Two exception classes
that are not related via subclassing are never equivalent, even if
they have the same name.
<a name="l2h-175"></a>
<a name="l2h-176"></a>

<P>
The built-in exceptions listed below can be generated by the
interpreter or built-in functions.  Except where mentioned, they have
an ``associated value'' indicating the detailed cause of the error.
This may be a string or a tuple containing several items of
information (e.g., an error code and a string explaining the code).
The associated value is the second argument to the <tt class=keyword>raise</tt>
statement.  For string exceptions, the associated value itself will be
stored in the variable named as the second argument of the
<tt class=keyword>except</tt> clause (if any).  For class exceptions, that variable
receives the exception instance.  If the exception class is derived
from the standard root class <tt class=exception>Exception</tt>, the associated
value is present as the exception instance's <tt class=member>args</tt> attribute,
and possibly on other attributes as well.
<a name="l2h-177"></a>

<P>
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.

<P>

<P>
The following exceptions are only used as base classes for other
exceptions.  When string-based standard exceptions are used, they
are tuples containing the directly derived classes.

<P>
<dl><dt><b><a name="l2h-149"><tt class=exception>Exception</tt></a></b>
<dd>
The root class for exceptions.  All built-in exceptions are derived
from this class.  All user-defined exceptions should also be derived
from this class, but this is not (yet) enforced.  The <tt class=function>str()</tt>
function, when applied to an instance of this class (or most derived
classes) returns the string value of the argument or arguments, or an
empty string if no arguments were given to the constructor.  When used
as a sequence, this accesses the arguments given to the constructor
(handy for backward compatibility with old code).  The arguments are
also available on the instance's <tt class=member>args</tt> attribute, as a tuple.
</dl>

<P>
<dl><dt><b><a name="l2h-150"><tt class=exception>StandardError</tt></a></b>
<dd>
The base class for all built-in exceptions except
<tt class=exception>SystemExit</tt>.  <tt class=exception>StandardError</tt> itself is derived
from the root class
<tt class=exception>Exception</tt>.
</dl>

<P>
<dl><dt><b><a name="l2h-151"><tt class=exception>ArithmeticError</tt></a></b>
<dd>
The base class for those built-in exceptions that are raised for
various arithmetic errors: <tt class=exception>OverflowError</tt>,
<tt class=exception>ZeroDivisionError</tt>, <tt class=exception>FloatingPointError</tt>.
</dl>

<P>
<dl><dt><b><a name="l2h-152"><tt class=exception>LookupError</tt></a></b>
<dd>
The base class for the exceptions that are raised when a key or
index used on a mapping or sequence is invalid: <tt class=exception>IndexError</tt>,
<tt class=exception>KeyError</tt>.
</dl>

<P>
<dl><dt><b><a name="l2h-153"><tt class=exception>EnvironmentError</tt></a></b>
<dd>
The base class for exceptions that
can occur outside the Python system: <tt class=exception>IOError</tt>,
<tt class=exception>OSError</tt>.  When exceptions of this type are created with a
2-tuple, the first item is available on the instance's <tt class=member>errno</tt>
attribute (it is assumed to be an error number), and the second item
is available on the <tt class=member>strerror</tt> attribute (it is usually the
associated error message).  The tuple itself is also available on the
<tt class=member>args</tt> attribute.

New in version 1.5.2.

<P>
When an <tt class=exception>EnvironmentError</tt> exception is instantiated with a
3-tuple, the first two items are available as above, while the third
item is available on the <tt class=member>filename</tt> attribute.  However, for
backwards compatibility, the <tt class=member>args</tt> attribute contains only a
2-tuple of the first two constructor arguments.

<P>
The <tt class=member>filename</tt> attribute is <tt>None</tt> when this exception is
created with other than 3 arguments.  The <tt class=member>errno</tt> and
<tt class=member>strerror</tt> attributes are also <tt>None</tt> when the instance was
created with other than 2 or 3 arguments.  In this last case,
<tt class=member>args</tt> contains the verbatim constructor arguments as a tuple.
</dl>

<P>

<P>
The following exceptions are the exceptions that are actually raised.
They are class objects, except when the <tt>-X</tt> option is used to
revert back to string-based standard exceptions.

<P>
<dl><dt><b><a name="l2h-154"><tt class=exception>AssertionError</tt></a></b>
<dd>
Raised when an <tt class=keyword>assert</tt> statement fails.
<a name="l2h-178"></a>
</dl>

<P>
<dl><dt><b><a name="l2h-155"><tt class=exception>AttributeError</tt></a></b>
<dd>
Raised when an attribute reference or assignment fails.  (When an
  object does not support attribute references or attribute assignments
  at all, <tt class=exception>TypeError</tt> is raised.)
</dl>

<P>
<dl><dt><b><a name="l2h-156"><tt class=exception>EOFError</tt></a></b>
<dd>
Raised when one of the built-in functions (<tt class=function>input()</tt> or
  <tt class=function>raw_input()</tt>) hits an end-of-file condition (EOF) without
  reading any data.
(N.B.: the <tt class=method>read()</tt> and <tt class=method>readline()</tt> methods of file
  objects return an empty string when they hit EOF.)
</dl>

<P>
<dl><dt><b><a name="l2h-157"><tt class=exception>FloatingPointError</tt></a></b>
<dd>
  Raised when a floating point operation fails.  This exception is
  always defined, but can only be raised when Python is configured
  with the <tt>-with-fpectl</tt> option, or the
  <tt class=constant>WANT_SIGFPE_HANDLER</tt> symbol is defined in the
  <tt class=file>config.h</tt> file.
</dl>

<P>
<dl><dt><b><a name="l2h-158"><tt class=exception>IOError</tt></a></b>
<dd>
Raised when an I/O operation (such as a <tt class=keyword>print</tt> statement,
  the built-in <tt class=function>open()</tt> function or a method of a file
  object) fails for an I/O-related reason, e.g., ``file not found'' or
  ``disk full''.

<P>
This class is derived from <tt class=exception>EnvironmentError</tt>.  See the
  discussion above for more information on exception instance
  attributes.
</dl>

<P>
<dl><dt><b><a name="l2h-159"><tt class=exception>ImportError</tt></a></b>
<dd>
Raised when an <tt class=keyword>import</tt> statement fails to find the module
  definition or when a <tt>from  ... import</tt> fails to find a
  name that is to be imported.
</dl>

<P>
<dl><dt><b><a name="l2h-160"><tt class=exception>IndexError</tt></a></b>
<dd>
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 a
  plain integer, <tt class=exception>TypeError</tt> is raised.)
</dl>

<P>
<dl><dt><b><a name="l2h-161"><tt class=exception>KeyError</tt></a></b>
<dd>
Raised when a mapping (dictionary) key is not found in the set of
  existing keys.
</dl>

<P>
<dl><dt><b><a name="l2h-162"><tt class=exception>KeyboardInterrupt</tt></a></b>
<dd>
  Raised when the user hits the interrupt key (normally
  <kbd>Control-C</kbd> or <kbd>DEL</kbd>).  During execution, a check for
  interrupts is made regularly.
Interrupts typed when a built-in function <tt class=function>input()</tt> or
  <tt class=function>raw_input()</tt>) is waiting for input also raise this
  exception.
</dl>

<P>
<dl><dt><b><a name="l2h-163"><tt class=exception>MemoryError</tt></a></b>
<dd>
  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 underlying memory management architecture
  (C's <tt class=cfunction>malloc()</tt> function), the 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.
</dl>

<P>
<dl><dt><b><a name="l2h-164"><tt class=exception>NameError</tt></a></b>
<dd>
  Raised when a local or global name is not found.  This applies only
  to unqualified names.  The associated value is the name that could
  not be found.
</dl>

<P>
<dl><dt><b><a name="l2h-165"><tt class=exception>NotImplementedError</tt></a></b>
<dd>
  This exception is derived from <tt class=exception>RuntimeError</tt>.  In user
  defined base classes, abstract methods should raise this exception
  when they require derived classes to override the method.
  
New in version 1.5.2.

</dl>

<P>
<dl><dt><b><a name="l2h-166"><tt class=exception>OSError</tt></a></b>
<dd>
 This class is derived from <tt class=exception>EnvironmentError</tt> and is used
  primarily as the <tt class=module><a href="module-os.html">os</a></tt> module's <tt>os.error</tt> exception.
  See <tt class=exception>EnvironmentError</tt> above for a description of the
  possible associated values.
  
New in version 1.5.2.

</dl>

<P>
<dl><dt><b><a name="l2h-167"><tt class=exception>OverflowError</tt></a></b>
<dd>
Raised when the result of an arithmetic operation is too large to be
  represented.  This cannot occur for long integers (which would rather
  raise <tt class=exception>MemoryError</tt> than give up).  Because of the lack of
  standardization of floating point exception handling in C, most
  floating point operations also aren't checked.  For plain integers,
  all operations that can overflow are checked except left shift, where
  typical applications prefer to drop bits than raise an exception.
</dl>

<P>
<dl><dt><b><a name="l2h-168"><tt class=exception>RuntimeError</tt></a></b>
<dd>
  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 wrong.  (This exception is mostly a relic from a
  previous version of the interpreter; it is not used very much any
  more.)
</dl>

<P>
<dl><dt><b><a name="l2h-169"><tt class=exception>SyntaxError</tt></a></b>
<dd>
Raised when the parser encounters a syntax error.  This may occur in
  an <tt class=keyword>import</tt> statement, in an <tt class=keyword>exec</tt> statement, in a call
  to the built-in function <tt class=function>eval()</tt> or <tt class=function>input()</tt>, or
  when reading the initial script or standard input (also
  interactively).

<P>
When class exceptions are used, instances of this class have
atttributes <tt class=member>filename</tt>, <tt class=member>lineno</tt>, <tt class=member>offset</tt> and
<tt class=member>text</tt> for easier access to the details; for string exceptions,
the associated value is usually a tuple of the form
<tt>(message, (filename, lineno, offset, text))</tt>.
For class exceptions, <tt class=function>str()</tt> returns only the message.
</dl>

<P>
<dl><dt><b><a name="l2h-170"><tt class=exception>SystemError</tt></a></b>
<dd>
  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 string of the Python
  interpreter (<tt>sys.version</tt>; 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.
</dl>

<P>
<dl><dt><b><a name="l2h-171"><tt class=exception>SystemExit</tt></a></b>
<dd>
This exception is raised by the <tt class=function>sys.exit()</tt> function.  When it
  is not handled, the Python interpreter exits; no stack traceback is
  printed.  If the associated value is a plain integer, it specifies the
  system exit status (passed to C's <tt class=cfunction>exit()</tt> function); if it is
  <tt>None</tt>, the exit status is zero; if it has another type (such as
  a string), the object's value is printed and the exit status is one.

<P>
When class exceptions are used, the instance has an attribute
<tt class=member>code</tt> which is set to the proposed exit status or error message
(defaulting to <tt>None</tt>).  Also, this exception derives directly
from <tt class=exception>Exception</tt> and not <tt class=exception>StandardError</tt>, since it 
is not technically an error.
  
  A call to <tt class=function>sys.exit()</tt> is translated into an exception so that
  clean-up handlers (<tt class=keyword>finally</tt> clauses of <tt class=keyword>try</tt> statements)
  can be executed, and so that a debugger can execute a script without
  running the risk of losing control.  The <tt class=function>os._exit()</tt> function
  can be used if it is absolutely positively necessary to exit
  immediately (e.g., after a <tt class=function>fork()</tt> in the child process).
</dl>

<P>
<dl><dt><b><a name="l2h-172"><tt class=exception>TypeError</tt></a></b>
<dd>
  Raised when a built-in operation or function is applied to an object
  of inappropriate type.  The associated value is a string giving
  details about the type mismatch.
</dl>

<P>
<dl><dt><b><a name="l2h-173"><tt class=exception>ValueError</tt></a></b>
<dd>
  Raised when a built-in operation or function receives an argument
  that has the right type but an inappropriate value, and the
  situation is not described by a more precise exception such as
  <tt class=exception>IndexError</tt>.
</dl>

<P>
<dl><dt><b><a name="l2h-174"><tt class=exception>ZeroDivisionError</tt></a></b>
<dd>
  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.
</dl>
<BR><HR><H4>Footnotes</H4>
<DL>
<DT><A NAME="foot2122">... strings</A><A NAME="foot2122"
 HREF="module-exceptions.html#tex2html7"><SUP>2.7</SUP></A>
<DD>For
forward-compatibility the new exceptions <tt class=exception>Exception</tt>,
<tt class=exception>LookupError</tt>,
<tt class=exception>ArithmeticError</tt>, <tt class=exception>EnvironmentError</tt>, and
<tt class=exception>StandardError</tt> are tuples.

</DL><p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="built-in-funcs.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="builtin.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="specialattrs.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="built-in-funcs.html">2.3 Built-in Functions</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="builtin.html">2. Built-in Types, Exceptions</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="specialattrs.html">2.1.8 Special Attributes</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
