blob: b785a7c725fadc4538e4be5372811367564be761 [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
64 +-- SystemError
65 +-- MemoryError
Barry Warsaw3e613ce1997-08-29 21:59:26 +000066"""
67
Guido van Rossumc56ba381997-09-16 18:42:04 +000068class Exception:
Guido van Rossum43389201999-01-15 04:03:46 +000069 """Proposed base class for all exceptions."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +000070 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000071 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +000072
73 def __str__(self):
Guido van Rossumc56ba381997-09-16 18:42:04 +000074 if not self.args:
Barry Warsaw3e613ce1997-08-29 21:59:26 +000075 return ''
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000076 elif len(self.args) == 1:
77 return str(self.args[0])
78 else:
79 return str(self.args)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000080
81 def __getitem__(self, i):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000082 return self.args[i]
Barry Warsaw3e613ce1997-08-29 21:59:26 +000083
Fred Drake596db311997-10-06 15:48:20 +000084class StandardError(Exception):
Guido van Rossum43389201999-01-15 04:03:46 +000085 """Base class for all standard Python exceptions."""
Fred Drake596db311997-10-06 15:48:20 +000086 pass
87
Barry Warsaw3e613ce1997-08-29 21:59:26 +000088class SyntaxError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +000089 """Invalid syntax."""
Guido van Rossumf394f561997-09-05 19:00:56 +000090 filename = lineno = offset = text = None
Guido van Rossumc56ba381997-09-16 18:42:04 +000091 msg = ""
92 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000093 self.args = args
94 if len(self.args) >= 1:
95 self.msg = self.args[0]
96 if len(self.args) == 2:
97 info = self.args[1]
98 try:
99 self.filename, self.lineno, self.offset, self.text = info
100 except:
101 pass
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000102 def __str__(self):
Guido van Rossumf394f561997-09-05 19:00:56 +0000103 return str(self.msg)
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000104
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000105class EnvironmentError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000106 """Base class for I/O related errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000107 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000108 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000109 self.errno = None
110 self.strerror = None
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000111 self.filename = None
112 if len(args) == 3:
113 # open() errors give third argument which is the filename. BUT,
114 # so common in-place unpacking doesn't break, e.g.:
115 #
116 # except IOError, (errno, strerror):
117 #
118 # we hack args so that it only contains two items. This also
119 # means we need our own __str__() which prints out the filename
120 # when it was supplied.
121 self.errno, self.strerror, self.filename = args
122 self.args = args[0:2]
Guido van Rossumc56ba381997-09-16 18:42:04 +0000123 if len(args) == 2:
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000124 # common case: PyErr_SetFromErrno()
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000125 self.errno, self.strerror = args
126
127 def __str__(self):
Guido van Rossumbe21d981998-08-11 18:01:32 +0000128 if self.filename is not None:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000129 return '[Errno %s] %s: %s' % (self.errno, self.strerror,
Guido van Rossumbe21d981998-08-11 18:01:32 +0000130 repr(self.filename))
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000131 elif self.errno and self.strerror:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000132 return '[Errno %s] %s' % (self.errno, self.strerror)
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000133 else:
134 return StandardError.__str__(self)
135
136class IOError(EnvironmentError):
Guido van Rossum43389201999-01-15 04:03:46 +0000137 """I/O operation failed."""
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000138 pass
139
140class OSError(EnvironmentError):
Guido van Rossum43389201999-01-15 04:03:46 +0000141 """OS system call failed."""
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000142 pass
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000143
Guido van Rossum1efac332000-02-17 15:12:01 +0000144class WindowsError(OSError):
145 """MS-Windows OS system call failed."""
146 pass
147
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000148class RuntimeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000149 """Unspecified run-time error."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000150 pass
151
Barry Warsaw8fe2a341998-12-01 18:36:30 +0000152class NotImplementedError(RuntimeError):
Guido van Rossum43389201999-01-15 04:03:46 +0000153 """Method or function hasn't been implemented yet."""
Barry Warsaw8fe2a341998-12-01 18:36:30 +0000154 pass
155
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000156class SystemError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000157 """Internal error in the Python interpreter.
158
159 Please report this to the Python maintainer, along with the traceback,
160 the Python version, and the hardware/OS platform and version."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000161 pass
162
163class EOFError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000164 """Read beyond end of file."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000165 pass
166
167class ImportError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000168 """Import can't find module, or can't find name in module."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000169 pass
170
171class TypeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000172 """Inappropriate argument type."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000173 pass
174
175class ValueError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000176 """Inappropriate argument value (of correct type)."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000177 pass
178
179class KeyboardInterrupt(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000180 """Program interrupted by user."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000181 pass
182
183class AssertionError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000184 """Assertion failed."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000185 pass
186
Barry Warsaw25131fa1997-09-16 21:50:59 +0000187class ArithmeticError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000188 """Base class for arithmetic errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000189 pass
190
Barry Warsaw25131fa1997-09-16 21:50:59 +0000191class OverflowError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000192 """Result too large to be represented."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000193 pass
194
Barry Warsaw25131fa1997-09-16 21:50:59 +0000195class FloatingPointError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000196 """Floating point operation failed."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000197 pass
198
Barry Warsaw25131fa1997-09-16 21:50:59 +0000199class ZeroDivisionError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000200 """Second argument to a division or modulo operation was zero."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000201 pass
202
203class LookupError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000204 """Base class for lookup errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000205 pass
206
207class IndexError(LookupError):
Guido van Rossum43389201999-01-15 04:03:46 +0000208 """Sequence index out of range."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000209 pass
210
211class KeyError(LookupError):
Guido van Rossum43389201999-01-15 04:03:46 +0000212 """Mapping key not found."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000213 pass
214
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000215class AttributeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000216 """Attribute not found."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000217 pass
218
219class NameError(StandardError):
Guido van Rossum87460821999-06-22 14:47:32 +0000220 """Name not found globally."""
221 pass
222
223class UnboundLocalError(NameError):
224 """Local name referenced but not bound to a value."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000225 pass
226
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000227class MemoryError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000228 """Out of memory."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000229 pass
Guido van Rossumc56ba381997-09-16 18:42:04 +0000230
231class SystemExit(Exception):
Guido van Rossum43389201999-01-15 04:03:46 +0000232 """Request to exit from the interpreter."""
Guido van Rossumc56ba381997-09-16 18:42:04 +0000233 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000234 self.args = args
Guido van Rossumc56ba381997-09-16 18:42:04 +0000235 if len(args) == 0:
236 self.code = None
237 elif len(args) == 1:
238 self.code = args[0]
239 else:
240 self.code = args