1 """Class based built-in exception hierarchy.
3 New with Python 1.5, all standard built-in exceptions are now class objects by
4 default. This gives Python's exception handling mechanism a more
5 object-oriented feel. Traditionally they were string objects. Python will
6 fallback to string based exceptions if the interpreter is invoked with the -X
7 option, or if some failure occurs during class exception initialization (in
8 this case a warning will be printed).
10 Most existing code should continue to work with class based exceptions. Some
11 tricky uses of IOError may break, but the most common uses should work.
13 Here is a rundown of the class hierarchy. You can change this by editing this
14 file, but it isn't recommended because the old string based exceptions won't
15 be kept in sync. The class names described here are expected to be found by
16 the bltinmodule.c file. If you add classes here, you must modify
17 bltinmodule.c or the exceptions won't be available in the __builtin__ module,
18 nor will they be accessible from C.
20 The classes with a `*' are new since Python 1.5. They are defined as tuples
21 containing the derived exceptions when string-based exceptions are used. If
22 you define your own class based exceptions, they should be derived from
32 +-- EnvironmentError(*)
40 | +-- NotImplementedError(*)
44 | +-- UnboundLocalError(*)
55 +-- ArithmeticError(*)
58 | +-- ZeroDivisionError
59 | +-- FloatingPointError
67 """Proposed base class for all exceptions."""
68 def __init__(self
, *args
):
74 elif len(self
.args
) == 1:
75 return str(self
.args
[0])
79 def __getitem__(self
, i
):
82 class StandardError(Exception):
83 """Base class for all standard Python exceptions."""
86 class SyntaxError(StandardError):
88 filename
= lineno
= offset
= text
= None
90 def __init__(self
, *args
):
92 if len(self
.args
) >= 1:
93 self
.msg
= self
.args
[0]
94 if len(self
.args
) == 2:
97 self
.filename
, self
.lineno
, self
.offset
, self
.text
= info
103 class EnvironmentError(StandardError):
104 """Base class for I/O related errors."""
105 def __init__(self
, *args
):
111 # open() errors give third argument which is the filename. BUT,
112 # so common in-place unpacking doesn't break, e.g.:
114 # except IOError, (errno, strerror):
116 # we hack args so that it only contains two items. This also
117 # means we need our own __str__() which prints out the filename
118 # when it was supplied.
119 self
.errno
, self
.strerror
, self
.filename
= args
120 self
.args
= args
[0:2]
122 # common case: PyErr_SetFromErrno()
123 self
.errno
, self
.strerror
= args
126 if self
.filename
is not None:
127 return '[Errno %s] %s: %s' % (self
.errno
, self
.strerror
,
129 elif self
.errno
and self
.strerror
:
130 return '[Errno %s] %s' % (self
.errno
, self
.strerror
)
132 return StandardError.__str
__(self
)
134 class IOError(EnvironmentError):
135 """I/O operation failed."""
138 class OSError(EnvironmentError):
139 """OS system call failed."""
142 class RuntimeError(StandardError):
143 """Unspecified run-time error."""
146 class NotImplementedError(RuntimeError):
147 """Method or function hasn't been implemented yet."""
150 class SystemError(StandardError):
151 """Internal error in the Python interpreter.
153 Please report this to the Python maintainer, along with the traceback,
154 the Python version, and the hardware/OS platform and version."""
157 class EOFError(StandardError):
158 """Read beyond end of file."""
161 class ImportError(StandardError):
162 """Import can't find module, or can't find name in module."""
165 class TypeError(StandardError):
166 """Inappropriate argument type."""
169 class ValueError(StandardError):
170 """Inappropriate argument value (of correct type)."""
173 class KeyboardInterrupt(StandardError):
174 """Program interrupted by user."""
177 class AssertionError(StandardError):
178 """Assertion failed."""
181 class ArithmeticError(StandardError):
182 """Base class for arithmetic errors."""
185 class OverflowError(ArithmeticError):
186 """Result too large to be represented."""
189 class FloatingPointError(ArithmeticError):
190 """Floating point operation failed."""
193 class ZeroDivisionError(ArithmeticError):
194 """Second argument to a division or modulo operation was zero."""
197 class LookupError(StandardError):
198 """Base class for lookup errors."""
201 class IndexError(LookupError):
202 """Sequence index out of range."""
205 class KeyError(LookupError):
206 """Mapping key not found."""
209 class AttributeError(StandardError):
210 """Attribute not found."""
213 class NameError(StandardError):
214 """Name not found globally."""
217 class UnboundLocalError(NameError):
218 """Local name referenced but not bound to a value."""
221 class MemoryError(StandardError):
225 class SystemExit(Exception):
226 """Request to exit from the interpreter."""
227 def __init__(self
, *args
):