1 """Class based built-in exception hierarchy.
3 This is a new feature whereby all the standard built-in exceptions,
4 traditionally string objects, are replaced with classes. This gives
5 Python's exception handling mechanism a more object-oriented feel.
7 Most existing code should continue to work with class based
8 exceptions. Some tricky uses of IOError may break, but the most
9 common uses should work.
11 To disable this feature, start the Python executable with the -X option.
13 Here is a rundown of the class hierarchy. You can change this by
14 editing this file, but it isn't recommended. The classes with a `*'
15 are new with this feature. They are defined as tuples containing the
16 derived exceptions when string-based exceptions are used.
38 +-- ArithmeticError(*)
41 | +-- ZeroDivisionError
42 | +-- FloatingPointError
50 def __init__(self
, *args
):
56 elif len(self
.args
) == 1:
57 return str(self
.args
[0])
61 def __getitem__(self
, i
):
64 class StandardError(Exception):
67 class SyntaxError(StandardError):
68 filename
= lineno
= offset
= text
= None
70 def __init__(self
, *args
):
72 if len(self
.args
) >= 1:
73 self
.msg
= self
.args
[0]
74 if len(self
.args
) == 2:
77 self
.filename
, self
.lineno
, self
.offset
, self
.text
= info
83 class EnvironmentError(StandardError):
84 """Base class for exceptions that occur outside the Python system.
85 Primarily used as a base class for OSError and IOError."""
86 def __init__(self
, *args
):
92 # open() errors give third argument which is the filename. BUT,
93 # so common in-place unpacking doesn't break, e.g.:
95 # except IOError, (errno, strerror):
97 # we hack args so that it only contains two items. This also
98 # means we need our own __str__() which prints out the filename
99 # when it was supplied.
100 self
.errno
, self
.strerror
, self
.filename
= args
101 self
.args
= args
[0:2]
103 # common case: PyErr_SetFromErrno()
104 self
.errno
, self
.strerror
= args
107 if self
.filename
is not None:
108 return '[Errno %s] %s: %s' % (self
.errno
, self
.strerror
,
110 elif self
.errno
and self
.strerror
:
111 return '[Errno %s] %s' % (self
.errno
, self
.strerror
)
113 return StandardError.__str
__(self
)
115 class IOError(EnvironmentError):
118 class OSError(EnvironmentError):
119 """Used by the posix module."""
122 class RuntimeError(StandardError):
125 class SystemError(StandardError):
128 class EOFError(StandardError):
131 class ImportError(StandardError):
134 class TypeError(StandardError):
137 class ValueError(StandardError):
140 class KeyboardInterrupt(StandardError):
143 class AssertionError(StandardError):
146 class ArithmeticError(StandardError):
149 class OverflowError(ArithmeticError):
152 class FloatingPointError(ArithmeticError):
155 class ZeroDivisionError(ArithmeticError):
158 class LookupError(StandardError):
161 class IndexError(LookupError):
164 class KeyError(LookupError):
167 class AttributeError(StandardError):
170 class NameError(StandardError):
173 class MemoryError(StandardError):
176 class SystemExit(Exception):
177 def __init__(self
, *args
):