blob: 43d1c2da40f34863c40371054537601786dc9ad1 [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
Barry Warsaw40db48c1999-02-24 00:27:39 +000014file, but it isn't recommended because the old string based exceptions won't
15be kept in sync. The class names described here are expected to be found by
16the bltinmodule.c file. If you add classes here, you must modify
17bltinmodule.c or the exceptions won't be available in the __builtin__ module,
18nor will they be accessible from C.
Barry Warsaw3e613ce1997-08-29 21:59:26 +000019
Barry Warsaw8fe2a341998-12-01 18:36:30 +000020The classes with a `*' are new since Python 1.5. They are defined as tuples
Barry Warsaw9195f551998-09-25 22:43:21 +000021containing the derived exceptions when string-based exceptions are used. If
22you define your own class based exceptions, they should be derived from
23Exception.
Barry Warsaw3e613ce1997-08-29 21:59:26 +000024
Guido van Rossumc56ba381997-09-16 18:42:04 +000025Exception(*)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000026 |
Barry Warsaw40db48c1999-02-24 00:27:39 +000027 +-- SystemExit
Guido van Rossumc56ba381997-09-16 18:42:04 +000028 +-- StandardError(*)
29 |
Guido van Rossumc56ba381997-09-16 18:42:04 +000030 +-- KeyboardInterrupt
31 +-- ImportError
Barry Warsaw9195f551998-09-25 22:43:21 +000032 +-- EnvironmentError(*)
33 | |
34 | +-- IOError
35 | +-- OSError(*)
Guido van Rossum1efac332000-02-17 15:12:01 +000036 | |
37 | +-- WindowsError(*)
Barry Warsaw9195f551998-09-25 22:43:21 +000038 |
Guido van Rossumc56ba381997-09-16 18:42:04 +000039 +-- EOFError
40 +-- RuntimeError
Barry Warsaw8fe2a341998-12-01 18:36:30 +000041 | |
42 | +-- NotImplementedError(*)
43 |
Guido van Rossumc56ba381997-09-16 18:42:04 +000044 +-- NameError
Guido van Rossum87460821999-06-22 14:47:32 +000045 | |
46 | +-- UnboundLocalError(*)
Barry Warsaw008edbf1999-08-19 21:17:08 +000047 |
Guido van Rossumc56ba381997-09-16 18:42:04 +000048 +-- AttributeError
49 +-- SyntaxError
50 +-- TypeError
51 +-- AssertionError
52 +-- LookupError(*)
53 | |
54 | +-- IndexError
55 | +-- KeyError
56 |
Barry Warsaw25131fa1997-09-16 21:50:59 +000057 +-- ArithmeticError(*)
Guido van Rossumc56ba381997-09-16 18:42:04 +000058 | |
59 | +-- OverflowError
60 | +-- ZeroDivisionError
61 | +-- FloatingPointError
62 |
63 +-- ValueError
Guido van Rossum21288ed2000-03-10 23:16:02 +000064 | |
65 | +-- UnicodeError(*)
66 |
Guido van Rossumc56ba381997-09-16 18:42:04 +000067 +-- SystemError
68 +-- MemoryError
Barry Warsaw3e613ce1997-08-29 21:59:26 +000069"""
70
Guido van Rossumc56ba381997-09-16 18:42:04 +000071class Exception:
Guido van Rossum43389201999-01-15 04:03:46 +000072 """Proposed base class for all exceptions."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +000073 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000074 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +000075
76 def __str__(self):
Guido van Rossumc56ba381997-09-16 18:42:04 +000077 if not self.args:
Barry Warsaw3e613ce1997-08-29 21:59:26 +000078 return ''
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000079 elif len(self.args) == 1:
80 return str(self.args[0])
81 else:
82 return str(self.args)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000083
84 def __getitem__(self, i):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000085 return self.args[i]
Barry Warsaw3e613ce1997-08-29 21:59:26 +000086
Fred Drake596db311997-10-06 15:48:20 +000087class StandardError(Exception):
Guido van Rossum43389201999-01-15 04:03:46 +000088 """Base class for all standard Python exceptions."""
Fred Drake596db311997-10-06 15:48:20 +000089 pass
90
Barry Warsaw3e613ce1997-08-29 21:59:26 +000091class SyntaxError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +000092 """Invalid syntax."""
Guido van Rossumf394f561997-09-05 19:00:56 +000093 filename = lineno = offset = text = None
Guido van Rossumc56ba381997-09-16 18:42:04 +000094 msg = ""
95 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000096 self.args = args
97 if len(self.args) >= 1:
98 self.msg = self.args[0]
99 if len(self.args) == 2:
100 info = self.args[1]
101 try:
102 self.filename, self.lineno, self.offset, self.text = info
103 except:
104 pass
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000105 def __str__(self):
Guido van Rossumf394f561997-09-05 19:00:56 +0000106 return str(self.msg)
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000107
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000108class EnvironmentError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000109 """Base class for I/O related errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000110 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000111 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000112 self.errno = None
113 self.strerror = None
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000114 self.filename = None
115 if len(args) == 3:
116 # open() errors give third argument which is the filename. BUT,
117 # so common in-place unpacking doesn't break, e.g.:
118 #
119 # except IOError, (errno, strerror):
120 #
121 # we hack args so that it only contains two items. This also
122 # means we need our own __str__() which prints out the filename
123 # when it was supplied.
124 self.errno, self.strerror, self.filename = args
125 self.args = args[0:2]
Guido van Rossumc56ba381997-09-16 18:42:04 +0000126 if len(args) == 2:
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000127 # common case: PyErr_SetFromErrno()
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000128 self.errno, self.strerror = args
129
130 def __str__(self):
Guido van Rossumbe21d981998-08-11 18:01:32 +0000131 if self.filename is not None:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000132 return '[Errno %s] %s: %s' % (self.errno, self.strerror,
Guido van Rossumbe21d981998-08-11 18:01:32 +0000133 repr(self.filename))
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000134 elif self.errno and self.strerror:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000135 return '[Errno %s] %s' % (self.errno, self.strerror)
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000136 else:
137 return StandardError.__str__(self)
138
139class IOError(EnvironmentError):
Guido van Rossum43389201999-01-15 04:03:46 +0000140 """I/O operation failed."""
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000141 pass
142
143class OSError(EnvironmentError):
Guido van Rossum43389201999-01-15 04:03:46 +0000144 """OS system call failed."""
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000145 pass
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000146
Guido van Rossum1efac332000-02-17 15:12:01 +0000147class WindowsError(OSError):
148 """MS-Windows OS system call failed."""
149 pass
150
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000151class RuntimeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000152 """Unspecified run-time error."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000153 pass
154
Barry Warsaw8fe2a341998-12-01 18:36:30 +0000155class NotImplementedError(RuntimeError):
Guido van Rossum43389201999-01-15 04:03:46 +0000156 """Method or function hasn't been implemented yet."""
Barry Warsaw8fe2a341998-12-01 18:36:30 +0000157 pass
158
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000159class SystemError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000160 """Internal error in the Python interpreter.
161
162 Please report this to the Python maintainer, along with the traceback,
163 the Python version, and the hardware/OS platform and version."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000164 pass
165
166class EOFError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000167 """Read beyond end of file."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000168 pass
169
170class ImportError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000171 """Import can't find module, or can't find name in module."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000172 pass
173
174class TypeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000175 """Inappropriate argument type."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000176 pass
177
178class ValueError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000179 """Inappropriate argument value (of correct type)."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000180 pass
181
182class KeyboardInterrupt(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000183 """Program interrupted by user."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000184 pass
185
186class AssertionError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000187 """Assertion failed."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000188 pass
189
Barry Warsaw25131fa1997-09-16 21:50:59 +0000190class ArithmeticError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000191 """Base class for arithmetic errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000192 pass
193
Barry Warsaw25131fa1997-09-16 21:50:59 +0000194class OverflowError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000195 """Result too large to be represented."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000196 pass
197
Barry Warsaw25131fa1997-09-16 21:50:59 +0000198class FloatingPointError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000199 """Floating point operation failed."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000200 pass
201
Barry Warsaw25131fa1997-09-16 21:50:59 +0000202class ZeroDivisionError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000203 """Second argument to a division or modulo operation was zero."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000204 pass
205
206class LookupError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000207 """Base class for lookup errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000208 pass
209
210class IndexError(LookupError):
Guido van Rossum43389201999-01-15 04:03:46 +0000211 """Sequence index out of range."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000212 pass
213
214class KeyError(LookupError):
Guido van Rossum43389201999-01-15 04:03:46 +0000215 """Mapping key not found."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000216 pass
217
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000218class AttributeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000219 """Attribute not found."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000220 pass
221
222class NameError(StandardError):
Guido van Rossum87460821999-06-22 14:47:32 +0000223 """Name not found globally."""
224 pass
225
226class UnboundLocalError(NameError):
227 """Local name referenced but not bound to a value."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000228 pass
229
Guido van Rossum21288ed2000-03-10 23:16:02 +0000230class UnicodeError(ValueError):
231 """Unicode related error."""
232 pass
233
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000234class MemoryError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000235 """Out of memory."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000236 pass
Guido van Rossumc56ba381997-09-16 18:42:04 +0000237
238class SystemExit(Exception):
Guido van Rossum43389201999-01-15 04:03:46 +0000239 """Request to exit from the interpreter."""
Guido van Rossumc56ba381997-09-16 18:42:04 +0000240 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000241 self.args = args
Guido van Rossumc56ba381997-09-16 18:42:04 +0000242 if len(args) == 0:
243 self.code = None
244 elif len(args) == 1:
245 self.code = args[0]
246 else:
247 self.code = args