⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module-exceptions.html

📁 一本很好的python的说明书,适合对python感兴趣的人
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<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" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="built-in-funcs.html" tppabs="http://www.python.org/doc/current/lib/built-in-funcs.html">
<LINK REL="previous" href="types.html" tppabs="http://www.python.org/doc/current/lib/types.html">
<LINK REL="up" href="builtin.html" tppabs="http://www.python.org/doc/current/lib/builtin.html">
<LINK REL="next" href="built-in-funcs.html" tppabs="http://www.python.org/doc/current/lib/built-in-funcs.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="specialattrs.html" tppabs="http://www.python.org/doc/current/lib/specialattrs.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="builtin.html" tppabs="http://www.python.org/doc/current/lib/builtin.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="built-in-funcs.html" tppabs="http://www.python.org/doc/current/lib/built-in-funcs.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="specialattrs.html" tppabs="http://www.python.org/doc/current/lib/specialattrs.html">2.1.8 Special Attributes</A>
<b class="navlabel">Up:</b> <a class="sectref" href="builtin.html" tppabs="http://www.python.org/doc/current/lib/builtin.html">2. Built-in Types, Exceptions</A>
<b class="navlabel">Next:</b> <a class="sectref" href="built-in-funcs.html" tppabs="http://www.python.org/doc/current/lib/built-in-funcs.html">2.3 Built-in Functions</A>
<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.  Though most
exceptions have been string objects in past versions of Python, in
Python 1.5 and newer versions, all standard exceptions have been
converted to class objects, and users are encouraged to do the same.
The exceptions are defined in the module <tt class="module">exceptions</tt>.  This
module never needs to be imported explicitly: the exceptions are
provided in the built-in namespace.

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

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

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

<P>
<dl><dt><b><a name='l2h-110'><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-111'><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-112'><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-113'><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-114'><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 <code>None</code> 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 <code>None</code> 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.

<P>
<dl><dt><b><a name='l2h-115'><tt class='exception'>AssertionError</tt></a></b>
<dd>

Raised when an <tt class="keyword">assert</tt> statement fails.
</dl>

<P>
<dl><dt><b><a name='l2h-116'><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-117'><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-118'><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 <b class="programopt">--with-fpectl</b> option, or the
  <tt class="constant">WANT_SIGFPE_HANDLER</tt> symbol is defined in the
  <span class="file">config.h</span> file.
</dl>

<P>
<dl><dt><b><a name='l2h-119'><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-120'><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 <code>from  ... import</code> fails to find a
  name that is to be imported.
</dl>

<P>
<dl><dt><b><a name='l2h-121'><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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -