blob: 12da416b8c342fd0f3ceb8caa53e194aa8d5b89d [file] [log] [blame]
Guido van Rossuma11cccc1997-10-06 20:19:59 +00001"""Class based built-in exception hierarchy.
2
Guido van Rossum64e736b1998-10-02 01:23:47 +00003New with Python 1.5, all standard built-in exceptions are now class objects by
4default. This gives Python's exception handling mechanism a more
5object-oriented feel. Traditionally they were string objects. Python will
6fallback to string based exceptions if the interpreter is invoked with the -X
7option, or if some failure occurs during class exception initialization (in
8this case a warning will be printed).
Guido van Rossuma11cccc1997-10-06 20:19:59 +00009
Guido van Rossum64e736b1998-10-02 01:23:47 +000010Most existing code should continue to work with class based exceptions. Some
11tricky uses of IOError may break, but the most common uses should work.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000012
Guido van Rossum64e736b1998-10-02 01:23:47 +000013Here is a rundown of the class hierarchy. You can change this by editing this
14file, but it isn't recommended. The class names described here are expected
Guido van Rossum7ea1d971998-12-22 13:50:33 +000015to be found by the bltinmodule.c file. If you add classes here, you must
16modify bltinmodule.c or the exceptions won't be available in the __builtin__
17module, nor will they be accessible from C.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000018
Guido van Rossum7ea1d971998-12-22 13:50:33 +000019The classes with a `*' are new since Python 1.5. They are defined as tuples
Guido van Rossum64e736b1998-10-02 01:23:47 +000020containing the derived exceptions when string-based exceptions are used. If
21you define your own class based exceptions, they should be derived from
22Exception.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000023
24Exception(*)
25 |
26 +-- StandardError(*)
27 |
28 +-- SystemExit
29 +-- KeyboardInterrupt
30 +-- ImportError
Guido van Rossum64e736b1998-10-02 01:23:47 +000031 +-- EnvironmentError(*)
32 | |
33 | +-- IOError
34 | +-- OSError(*)
35 |
Guido van Rossuma11cccc1997-10-06 20:19:59 +000036 +-- EOFError
37 +-- RuntimeError
Guido van Rossum7ea1d971998-12-22 13:50:33 +000038 | |
39 | +-- NotImplementedError(*)
40 |
Guido van Rossuma11cccc1997-10-06 20:19:59 +000041 +-- NameError
42 +-- AttributeError
43 +-- SyntaxError
44 +-- TypeError
45 +-- AssertionError
46 +-- LookupError(*)
47 | |
48 | +-- IndexError
49 | +-- KeyError
50 |
51 +-- ArithmeticError(*)
52 | |
53 | +-- OverflowError
54 | +-- ZeroDivisionError
55 | +-- FloatingPointError
56 |
57 +-- ValueError
58 +-- SystemError
59 +-- MemoryError
60"""
61
62class Exception:
63 def __init__(self, *args):
Guido van Rossum548703a1998-03-26 22:14:20 +000064 self.args = args
Guido van Rossuma11cccc1997-10-06 20:19:59 +000065
66 def __str__(self):
67 if not self.args:
68 return ''
Guido van Rossum548703a1998-03-26 22:14:20 +000069 elif len(self.args) == 1:
70 return str(self.args[0])
71 else:
72 return str(self.args)
Guido van Rossuma11cccc1997-10-06 20:19:59 +000073
74 def __getitem__(self, i):
Guido van Rossum548703a1998-03-26 22:14:20 +000075 return self.args[i]
Guido van Rossuma11cccc1997-10-06 20:19:59 +000076
77class StandardError(Exception):
78 pass
79
80class SyntaxError(StandardError):
81 filename = lineno = offset = text = None
82 msg = ""
83 def __init__(self, *args):
Guido van Rossum548703a1998-03-26 22:14:20 +000084 self.args = args
85 if len(self.args) >= 1:
86 self.msg = self.args[0]
87 if len(self.args) == 2:
88 info = self.args[1]
89 try:
90 self.filename, self.lineno, self.offset, self.text = info
91 except:
92 pass
Guido van Rossuma11cccc1997-10-06 20:19:59 +000093 def __str__(self):
94 return str(self.msg)
95
Guido van Rossume03c0501998-08-12 02:38:11 +000096class EnvironmentError(StandardError):
97 """Base class for exceptions that occur outside the Python system.
98 Primarily used as a base class for OSError and IOError."""
Guido van Rossuma11cccc1997-10-06 20:19:59 +000099 def __init__(self, *args):
Guido van Rossum548703a1998-03-26 22:14:20 +0000100 self.args = args
Guido van Rossuma11cccc1997-10-06 20:19:59 +0000101 self.errno = None
102 self.strerror = None
Guido van Rossume03c0501998-08-12 02:38:11 +0000103 self.filename = None
104 if len(args) == 3:
105 # open() errors give third argument which is the filename. BUT,
106 # so common in-place unpacking doesn't break, e.g.:
107 #
108 # except IOError, (errno, strerror):
109 #
110 # we hack args so that it only contains two items. This also
111 # means we need our own __str__() which prints out the filename
112 # when it was supplied.
113 self.errno, self.strerror, self.filename = args
114 self.args = args[0:2]
Guido van Rossuma11cccc1997-10-06 20:19:59 +0000115 if len(args) == 2:
116 # common case: PyErr_SetFromErrno()
Guido van Rossume03c0501998-08-12 02:38:11 +0000117 self.errno, self.strerror = args
118
119 def __str__(self):
120 if self.filename is not None:
121 return '[Errno %s] %s: %s' % (self.errno, self.strerror,
122 repr(self.filename))
123 elif self.errno and self.strerror:
124 return '[Errno %s] %s' % (self.errno, self.strerror)
125 else:
126 return StandardError.__str__(self)
127
128class IOError(EnvironmentError):
129 pass
130
131class OSError(EnvironmentError):
132 """Used by the posix module."""
133 pass
Guido van Rossuma11cccc1997-10-06 20:19:59 +0000134
135class RuntimeError(StandardError):
136 pass
137
Guido van Rossum7ea1d971998-12-22 13:50:33 +0000138class NotImplementedError(RuntimeError):
139 pass
140
Guido van Rossuma11cccc1997-10-06 20:19:59 +0000141class SystemError(StandardError):
142 pass
143
144class EOFError(StandardError):
145 pass
146
147class ImportError(StandardError):
148 pass
149
150class TypeError(StandardError):
151 pass
152
153class ValueError(StandardError):
154 pass
155
156class KeyboardInterrupt(StandardError):
157 pass
158
159class AssertionError(StandardError):
160 pass
161
162class ArithmeticError(StandardError):
163 pass
164
165class OverflowError(ArithmeticError):
166 pass
167
168class FloatingPointError(ArithmeticError):
169 pass
170
171class ZeroDivisionError(ArithmeticError):
172 pass
173
174class LookupError(StandardError):
175 pass
176
177class IndexError(LookupError):
178 pass
179
180class KeyError(LookupError):
181 pass
182
183class AttributeError(StandardError):
184 pass
185
186class NameError(StandardError):
187 pass
188
189class MemoryError(StandardError):
190 pass
191
192class SystemExit(Exception):
193 def __init__(self, *args):
Guido van Rossum548703a1998-03-26 22:14:20 +0000194 self.args = args
Guido van Rossuma11cccc1997-10-06 20:19:59 +0000195 if len(args) == 0:
196 self.code = None
197 elif len(args) == 1:
198 self.code = args[0]
199 else:
200 self.code = args