blob: 12da416b8c342fd0f3ceb8caa53e194aa8d5b89d [file] [log] [blame]
Barry Warsaw3e613ce1997-08-29 21:59:26 +00001"""Class based built-in exception hierarchy.
2
Barry Warsaw9195f551998-09-25 22:43:21 +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).
Barry Warsaw3e613ce1997-08-29 21:59:26 +00009
Barry Warsaw9195f551998-09-25 22:43:21 +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.
Barry Warsaw3e613ce1997-08-29 21:59:26 +000012
Barry Warsaw9195f551998-09-25 22:43:21 +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
Barry Warsaw8fe2a341998-12-01 18:36:30 +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.
Barry Warsaw3e613ce1997-08-29 21:59:26 +000018
Barry Warsaw8fe2a341998-12-01 18:36:30 +000019The classes with a `*' are new since Python 1.5. They are defined as tuples
Barry Warsaw9195f551998-09-25 22:43:21 +000020containing the derived exceptions when string-based exceptions are used. If
21you define your own class based exceptions, they should be derived from
22Exception.
Barry Warsaw3e613ce1997-08-29 21:59:26 +000023
Guido van Rossumc56ba381997-09-16 18:42:04 +000024Exception(*)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000025 |
Guido van Rossumc56ba381997-09-16 18:42:04 +000026 +-- StandardError(*)
27 |
28 +-- SystemExit
29 +-- KeyboardInterrupt
30 +-- ImportError
Barry Warsaw9195f551998-09-25 22:43:21 +000031 +-- EnvironmentError(*)
32 | |
33 | +-- IOError
34 | +-- OSError(*)
35 |
Guido van Rossumc56ba381997-09-16 18:42:04 +000036 +-- EOFError
37 +-- RuntimeError
Barry Warsaw8fe2a341998-12-01 18:36:30 +000038 | |
39 | +-- NotImplementedError(*)
40 |
Guido van Rossumc56ba381997-09-16 18:42:04 +000041 +-- NameError
42 +-- AttributeError
43 +-- SyntaxError
44 +-- TypeError
45 +-- AssertionError
46 +-- LookupError(*)
47 | |
48 | +-- IndexError
49 | +-- KeyError
50 |
Barry Warsaw25131fa1997-09-16 21:50:59 +000051 +-- ArithmeticError(*)
Guido van Rossumc56ba381997-09-16 18:42:04 +000052 | |
53 | +-- OverflowError
54 | +-- ZeroDivisionError
55 | +-- FloatingPointError
56 |
57 +-- ValueError
58 +-- SystemError
59 +-- MemoryError
Barry Warsaw3e613ce1997-08-29 21:59:26 +000060"""
61
Guido van Rossumc56ba381997-09-16 18:42:04 +000062class Exception:
Barry Warsaw3e613ce1997-08-29 21:59:26 +000063 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000064 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +000065
66 def __str__(self):
Guido van Rossumc56ba381997-09-16 18:42:04 +000067 if not self.args:
Barry Warsaw3e613ce1997-08-29 21:59:26 +000068 return ''
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000069 elif len(self.args) == 1:
70 return str(self.args[0])
71 else:
72 return str(self.args)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000073
74 def __getitem__(self, i):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000075 return self.args[i]
Barry Warsaw3e613ce1997-08-29 21:59:26 +000076
Fred Drake596db311997-10-06 15:48:20 +000077class StandardError(Exception):
78 pass
79
Barry Warsaw3e613ce1997-08-29 21:59:26 +000080class SyntaxError(StandardError):
Guido van Rossumf394f561997-09-05 19:00:56 +000081 filename = lineno = offset = text = None
Guido van Rossumc56ba381997-09-16 18:42:04 +000082 msg = ""
83 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +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
Barry Warsaw3e613ce1997-08-29 21:59:26 +000093 def __str__(self):
Guido van Rossumf394f561997-09-05 19:00:56 +000094 return str(self.msg)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000095
Barry Warsaw2dfe4de1998-07-23 16:03:46 +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."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +000099 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000100 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000101 self.errno = None
102 self.strerror = None
Barry Warsaw2dfe4de1998-07-23 16:03:46 +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 Rossumc56ba381997-09-16 18:42:04 +0000115 if len(args) == 2:
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000116 # common case: PyErr_SetFromErrno()
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000117 self.errno, self.strerror = args
118
119 def __str__(self):
Guido van Rossumbe21d981998-08-11 18:01:32 +0000120 if self.filename is not None:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000121 return '[Errno %s] %s: %s' % (self.errno, self.strerror,
Guido van Rossumbe21d981998-08-11 18:01:32 +0000122 repr(self.filename))
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000123 elif self.errno and self.strerror:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000124 return '[Errno %s] %s' % (self.errno, self.strerror)
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000125 else:
126 return StandardError.__str__(self)
127
128class IOError(EnvironmentError):
129 pass
130
131class OSError(EnvironmentError):
132 """Used by the posix module."""
133 pass
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000134
135class RuntimeError(StandardError):
136 pass
137
Barry Warsaw8fe2a341998-12-01 18:36:30 +0000138class NotImplementedError(RuntimeError):
139 pass
140
Barry Warsaw3e613ce1997-08-29 21:59:26 +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
Barry Warsaw25131fa1997-09-16 21:50:59 +0000162class ArithmeticError(StandardError):
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000163 pass
164
Barry Warsaw25131fa1997-09-16 21:50:59 +0000165class OverflowError(ArithmeticError):
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000166 pass
167
Barry Warsaw25131fa1997-09-16 21:50:59 +0000168class FloatingPointError(ArithmeticError):
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000169 pass
170
Barry Warsaw25131fa1997-09-16 21:50:59 +0000171class ZeroDivisionError(ArithmeticError):
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000172 pass
173
174class LookupError(StandardError):
175 pass
176
177class IndexError(LookupError):
178 pass
179
180class KeyError(LookupError):
181 pass
182
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000183class AttributeError(StandardError):
184 pass
185
186class NameError(StandardError):
187 pass
188
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000189class MemoryError(StandardError):
190 pass
Guido van Rossumc56ba381997-09-16 18:42:04 +0000191
192class SystemExit(Exception):
193 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000194 self.args = args
Guido van Rossumc56ba381997-09-16 18:42:04 +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