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. The class names described here are expected
15 to be found by the bltinmodule.c file.
17 The classes with a `*' are new as of Python 1.5. They are defined as tuples
18 containing the derived exceptions when string-based exceptions are used. If
19 you define your own class based exceptions, they should be derived from
29 +-- EnvironmentError(*)
46 +-- ArithmeticError(*)
49 | +-- ZeroDivisionError
50 | +-- FloatingPointError
58 def __init__(self
, *args
):
64 elif len(self
.args
) == 1:
65 return str(self
.args
[0])
69 def __getitem__(self
, i
):
72 class StandardError(Exception):
75 class SyntaxError(StandardError):
76 filename
= lineno
= offset
= text
= None
78 def __init__(self
, *args
):
80 if len(self
.args
) >= 1:
81 self
.msg
= self
.args
[0]
82 if len(self
.args
) == 2:
85 self
.filename
, self
.lineno
, self
.offset
, self
.text
= info
91 class EnvironmentError(StandardError):
92 """Base class for exceptions that occur outside the Python system.
93 Primarily used as a base class for OSError and IOError."""
94 def __init__(self
, *args
):
100 # open() errors give third argument which is the filename. BUT,
101 # so common in-place unpacking doesn't break, e.g.:
103 # except IOError, (errno, strerror):
105 # we hack args so that it only contains two items. This also
106 # means we need our own __str__() which prints out the filename
107 # when it was supplied.
108 self
.errno
, self
.strerror
, self
.filename
= args
109 self
.args
= args
[0:2]
111 # common case: PyErr_SetFromErrno()
112 self
.errno
, self
.strerror
= args
115 if self
.filename
is not None:
116 return '[Errno %s] %s: %s' % (self
.errno
, self
.strerror
,
118 elif self
.errno
and self
.strerror
:
119 return '[Errno %s] %s' % (self
.errno
, self
.strerror
)
121 return StandardError.__str
__(self
)
123 class IOError(EnvironmentError):
126 class OSError(EnvironmentError):
127 """Used by the posix module."""
130 class RuntimeError(StandardError):
133 class SystemError(StandardError):
136 class EOFError(StandardError):
139 class ImportError(StandardError):
142 class TypeError(StandardError):
145 class ValueError(StandardError):
148 class KeyboardInterrupt(StandardError):
151 class AssertionError(StandardError):
154 class ArithmeticError(StandardError):
157 class OverflowError(ArithmeticError):
160 class FloatingPointError(ArithmeticError):
163 class ZeroDivisionError(ArithmeticError):
166 class LookupError(StandardError):
169 class IndexError(LookupError):
172 class KeyError(LookupError):
175 class AttributeError(StandardError):
178 class NameError(StandardError):
181 class MemoryError(StandardError):
184 class SystemExit(Exception):
185 def __init__(self
, *args
):