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(*)
52 +-- ArithmeticError(*)
55 | +-- ZeroDivisionError
56 | +-- FloatingPointError
64 """Proposed base class for all exceptions."""
65 def __init__(self
, *args
):
71 elif len(self
.args
) == 1:
72 return str(self
.args
[0])
76 def __getitem__(self
, i
):
79 class StandardError(Exception):
80 """Base class for all standard Python exceptions."""
83 class SyntaxError(StandardError):
85 filename
= lineno
= offset
= text
= None
87 def __init__(self
, *args
):
89 if len(self
.args
) >= 1:
90 self
.msg
= self
.args
[0]
91 if len(self
.args
) == 2:
94 self
.filename
, self
.lineno
, self
.offset
, self
.text
= info
100 class EnvironmentError(StandardError):
101 """Base class for I/O related errors."""
102 def __init__(self
, *args
):
108 # open() errors give third argument which is the filename. BUT,
109 # so common in-place unpacking doesn't break, e.g.:
111 # except IOError, (errno, strerror):
113 # we hack args so that it only contains two items. This also
114 # means we need our own __str__() which prints out the filename
115 # when it was supplied.
116 self
.errno
, self
.strerror
, self
.filename
= args
117 self
.args
= args
[0:2]
119 # common case: PyErr_SetFromErrno()
120 self
.errno
, self
.strerror
= args
123 if self
.filename
is not None:
124 return '[Errno %s] %s: %s' % (self
.errno
, self
.strerror
,
126 elif self
.errno
and self
.strerror
:
127 return '[Errno %s] %s' % (self
.errno
, self
.strerror
)
129 return StandardError.__str
__(self
)
131 class IOError(EnvironmentError):
132 """I/O operation failed."""
135 class OSError(EnvironmentError):
136 """OS system call failed."""
139 class RuntimeError(StandardError):
140 """Unspecified run-time error."""
143 class NotImplementedError(RuntimeError):
144 """Method or function hasn't been implemented yet."""
147 class SystemError(StandardError):
148 """Internal error in the Python interpreter.
150 Please report this to the Python maintainer, along with the traceback,
151 the Python version, and the hardware/OS platform and version."""
154 class EOFError(StandardError):
155 """Read beyond end of file."""
158 class ImportError(StandardError):
159 """Import can't find module, or can't find name in module."""
162 class TypeError(StandardError):
163 """Inappropriate argument type."""
166 class ValueError(StandardError):
167 """Inappropriate argument value (of correct type)."""
170 class KeyboardInterrupt(StandardError):
171 """Program interrupted by user."""
174 class AssertionError(StandardError):
175 """Assertion failed."""
178 class ArithmeticError(StandardError):
179 """Base class for arithmetic errors."""
182 class OverflowError(ArithmeticError):
183 """Result too large to be represented."""
186 class FloatingPointError(ArithmeticError):
187 """Floating point operation failed."""
190 class ZeroDivisionError(ArithmeticError):
191 """Second argument to a division or modulo operation was zero."""
194 class LookupError(StandardError):
195 """Base class for lookup errors."""
198 class IndexError(LookupError):
199 """Sequence index out of range."""
202 class KeyError(LookupError):
203 """Mapping key not found."""
206 class AttributeError(StandardError):
207 """Attribute not found."""
210 class NameError(StandardError):
211 """Name not found locally or globally."""
214 class MemoryError(StandardError):
218 class SystemExit(Exception):
219 """Request to exit from the interpreter."""
220 def __init__(self
, *args
):