blob: af752d915e41c0536254fd6c4350ba8dc6ce6823 [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(*)
36 |
Guido van Rossumc56ba381997-09-16 18:42:04 +000037 +-- EOFError
38 +-- RuntimeError
Barry Warsaw8fe2a341998-12-01 18:36:30 +000039 | |
40 | +-- NotImplementedError(*)
41 |
Guido van Rossumc56ba381997-09-16 18:42:04 +000042 +-- NameError
Guido van Rossum87460821999-06-22 14:47:32 +000043 | |
44 | +-- UnboundLocalError(*)
Guido van Rossumc56ba381997-09-16 18:42:04 +000045 +-- AttributeError
46 +-- SyntaxError
47 +-- TypeError
48 +-- AssertionError
49 +-- LookupError(*)
50 | |
51 | +-- IndexError
52 | +-- KeyError
53 |
Barry Warsaw25131fa1997-09-16 21:50:59 +000054 +-- ArithmeticError(*)
Guido van Rossumc56ba381997-09-16 18:42:04 +000055 | |
56 | +-- OverflowError
57 | +-- ZeroDivisionError
58 | +-- FloatingPointError
59 |
60 +-- ValueError
61 +-- SystemError
62 +-- MemoryError
Barry Warsaw3e613ce1997-08-29 21:59:26 +000063"""
64
Guido van Rossumc56ba381997-09-16 18:42:04 +000065class Exception:
Guido van Rossum43389201999-01-15 04:03:46 +000066 """Proposed base class for all exceptions."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +000067 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000068 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +000069
70 def __str__(self):
Guido van Rossumc56ba381997-09-16 18:42:04 +000071 if not self.args:
Barry Warsaw3e613ce1997-08-29 21:59:26 +000072 return ''
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000073 elif len(self.args) == 1:
74 return str(self.args[0])
75 else:
76 return str(self.args)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000077
78 def __getitem__(self, i):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000079 return self.args[i]
Barry Warsaw3e613ce1997-08-29 21:59:26 +000080
Fred Drake596db311997-10-06 15:48:20 +000081class StandardError(Exception):
Guido van Rossum43389201999-01-15 04:03:46 +000082 """Base class for all standard Python exceptions."""
Fred Drake596db311997-10-06 15:48:20 +000083 pass
84
Barry Warsaw3e613ce1997-08-29 21:59:26 +000085class SyntaxError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +000086 """Invalid syntax."""
Guido van Rossumf394f561997-09-05 19:00:56 +000087 filename = lineno = offset = text = None
Guido van Rossumc56ba381997-09-16 18:42:04 +000088 msg = ""
89 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000090 self.args = args
91 if len(self.args) >= 1:
92 self.msg = self.args[0]
93 if len(self.args) == 2:
94 info = self.args[1]
95 try:
96 self.filename, self.lineno, self.offset, self.text = info
97 except:
98 pass
Barry Warsaw3e613ce1997-08-29 21:59:26 +000099 def __str__(self):
Guido van Rossumf394f561997-09-05 19:00:56 +0000100 return str(self.msg)
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000101
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000102class EnvironmentError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000103 """Base class for I/O related errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000104 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000105 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000106 self.errno = None
107 self.strerror = None
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000108 self.filename = None
109 if len(args) == 3:
110 # open() errors give third argument which is the filename. BUT,
111 # so common in-place unpacking doesn't break, e.g.:
112 #
113 # except IOError, (errno, strerror):
114 #
115 # we hack args so that it only contains two items. This also
116 # means we need our own __str__() which prints out the filename
117 # when it was supplied.
118 self.errno, self.strerror, self.filename = args
119 self.args = args[0:2]
Guido van Rossumc56ba381997-09-16 18:42:04 +0000120 if len(args) == 2:
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000121 # common case: PyErr_SetFromErrno()
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000122 self.errno, self.strerror = args
123
124 def __str__(self):
Guido van Rossumbe21d981998-08-11 18:01:32 +0000125 if self.filename is not None:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000126 return '[Errno %s] %s: %s' % (self.errno, self.strerror,
Guido van Rossumbe21d981998-08-11 18:01:32 +0000127 repr(self.filename))
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000128 elif self.errno and self.strerror:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000129 return '[Errno %s] %s' % (self.errno, self.strerror)
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000130 else:
131 return StandardError.__str__(self)
132
133class IOError(EnvironmentError):
Guido van Rossum43389201999-01-15 04:03:46 +0000134 """I/O operation failed."""
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000135 pass
136
137class OSError(EnvironmentError):
Guido van Rossum43389201999-01-15 04:03:46 +0000138 """OS system call failed."""
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000139 pass
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000140
141class RuntimeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000142 """Unspecified run-time error."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000143 pass
144
Barry Warsaw8fe2a341998-12-01 18:36:30 +0000145class NotImplementedError(RuntimeError):
Guido van Rossum43389201999-01-15 04:03:46 +0000146 """Method or function hasn't been implemented yet."""
Barry Warsaw8fe2a341998-12-01 18:36:30 +0000147 pass
148
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000149class SystemError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000150 """Internal error in the Python interpreter.
151
152 Please report this to the Python maintainer, along with the traceback,
153 the Python version, and the hardware/OS platform and version."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000154 pass
155
156class EOFError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000157 """Read beyond end of file."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000158 pass
159
160class ImportError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000161 """Import can't find module, or can't find name in module."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000162 pass
163
164class TypeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000165 """Inappropriate argument type."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000166 pass
167
168class ValueError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000169 """Inappropriate argument value (of correct type)."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000170 pass
171
172class KeyboardInterrupt(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000173 """Program interrupted by user."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000174 pass
175
176class AssertionError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000177 """Assertion failed."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000178 pass
179
Barry Warsaw25131fa1997-09-16 21:50:59 +0000180class ArithmeticError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000181 """Base class for arithmetic errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000182 pass
183
Barry Warsaw25131fa1997-09-16 21:50:59 +0000184class OverflowError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000185 """Result too large to be represented."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000186 pass
187
Barry Warsaw25131fa1997-09-16 21:50:59 +0000188class FloatingPointError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000189 """Floating point operation failed."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000190 pass
191
Barry Warsaw25131fa1997-09-16 21:50:59 +0000192class ZeroDivisionError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000193 """Second argument to a division or modulo operation was zero."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000194 pass
195
196class LookupError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000197 """Base class for lookup errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000198 pass
199
200class IndexError(LookupError):
Guido van Rossum43389201999-01-15 04:03:46 +0000201 """Sequence index out of range."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000202 pass
203
204class KeyError(LookupError):
Guido van Rossum43389201999-01-15 04:03:46 +0000205 """Mapping key not found."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000206 pass
207
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000208class AttributeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000209 """Attribute not found."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000210 pass
211
212class NameError(StandardError):
Guido van Rossum87460821999-06-22 14:47:32 +0000213 """Name not found globally."""
214 pass
215
216class UnboundLocalError(NameError):
217 """Local name referenced but not bound to a value."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000218 pass
219
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000220class MemoryError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000221 """Out of memory."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000222 pass
Guido van Rossumc56ba381997-09-16 18:42:04 +0000223
224class SystemExit(Exception):
Guido van Rossum43389201999-01-15 04:03:46 +0000225 """Request to exit from the interpreter."""
Guido van Rossumc56ba381997-09-16 18:42:04 +0000226 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000227 self.args = args
Guido van Rossumc56ba381997-09-16 18:42:04 +0000228 if len(args) == 0:
229 self.code = None
230 elif len(args) == 1:
231 self.code = args[0]
232 else:
233 self.code = args