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(*)
42 | +-- NotImplementedError(*)
46 | +-- UnboundLocalError(*)
57 +-- ArithmeticError(*)
60 | +-- ZeroDivisionError
61 | +-- FloatingPointError
72 """Proposed base class for all exceptions."""
73 def __init__(self
, *args
):
79 elif len(self
.args
) == 1:
80 return str(self
.args
[0])
84 def __getitem__(self
, i
):
87 class StandardError(Exception):
88 """Base class for all standard Python exceptions."""
91 class SyntaxError(StandardError):
93 filename
= lineno
= offset
= text
= None
95 def __init__(self
, *args
):
97 if len(self
.args
) >= 1:
98 self
.msg
= self
.args
[0]
99 if len(self
.args
) == 2:
102 self
.filename
, self
.lineno
, self
.offset
, self
.text
= info
108 class EnvironmentError(StandardError):
109 """Base class for I/O related errors."""
110 def __init__(self
, *args
):
116 # open() errors give third argument which is the filename. BUT,
117 # so common in-place unpacking doesn't break, e.g.:
119 # except IOError, (errno, strerror):
121 # we hack args so that it only contains two items. This also
122 # means we need our own __str__() which prints out the filename
123 # when it was supplied.
124 self
.errno
, self
.strerror
, self
.filename
= args
125 self
.args
= args
[0:2]
127 # common case: PyErr_SetFromErrno()
128 self
.errno
, self
.strerror
= args
131 if self
.filename
is not None:
132 return '[Errno %s] %s: %s' % (self
.errno
, self
.strerror
,
134 elif self
.errno
and self
.strerror
:
135 return '[Errno %s] %s' % (self
.errno
, self
.strerror
)
137 return StandardError.__str
__(self
)
139 class IOError(EnvironmentError):
140 """I/O operation failed."""
143 class OSError(EnvironmentError):
144 """OS system call failed."""
147 class WindowsError(OSError):
148 """MS-Windows OS system call failed."""
151 class RuntimeError(StandardError):
152 """Unspecified run-time error."""
155 class NotImplementedError(RuntimeError):
156 """Method or function hasn't been implemented yet."""
159 class SystemError(StandardError):
160 """Internal error in the Python interpreter.
162 Please report this to the Python maintainer, along with the traceback,
163 the Python version, and the hardware/OS platform and version."""
166 class EOFError(StandardError):
167 """Read beyond end of file."""
170 class ImportError(StandardError):
171 """Import can't find module, or can't find name in module."""
174 class TypeError(StandardError):
175 """Inappropriate argument type."""
178 class ValueError(StandardError):
179 """Inappropriate argument value (of correct type)."""
182 class KeyboardInterrupt(StandardError):
183 """Program interrupted by user."""
186 class AssertionError(StandardError):
187 """Assertion failed."""
190 class ArithmeticError(StandardError):
191 """Base class for arithmetic errors."""
194 class OverflowError(ArithmeticError):
195 """Result too large to be represented."""
198 class FloatingPointError(ArithmeticError):
199 """Floating point operation failed."""
202 class ZeroDivisionError(ArithmeticError):
203 """Second argument to a division or modulo operation was zero."""
206 class LookupError(StandardError):
207 """Base class for lookup errors."""
210 class IndexError(LookupError):
211 """Sequence index out of range."""
214 class KeyError(LookupError):
215 """Mapping key not found."""
218 class AttributeError(StandardError):
219 """Attribute not found."""
222 class NameError(StandardError):
223 """Name not found globally."""
226 class UnboundLocalError(NameError):
227 """Local name referenced but not bound to a value."""
230 class UnicodeError(ValueError):
231 """Unicode related error."""
234 class MemoryError(StandardError):
238 class SystemExit(Exception):
239 """Request to exit from the interpreter."""
240 def __init__(self
, *args
):