blob: 56eed92d804be5e5547ce584f4c2506316a682a6 [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(*)
Barry Warsaw008edbf1999-08-19 21:17:08 +000045 |
Guido van Rossumc56ba381997-09-16 18:42:04 +000046 +-- AttributeError
47 +-- SyntaxError
48 +-- TypeError
49 +-- AssertionError
50 +-- LookupError(*)
51 | |
52 | +-- IndexError
53 | +-- KeyError
54 |
Barry Warsaw25131fa1997-09-16 21:50:59 +000055 +-- ArithmeticError(*)
Guido van Rossumc56ba381997-09-16 18:42:04 +000056 | |
57 | +-- OverflowError
58 | +-- ZeroDivisionError
59 | +-- FloatingPointError
60 |
61 +-- ValueError
62 +-- SystemError
63 +-- MemoryError
Barry Warsaw3e613ce1997-08-29 21:59:26 +000064"""
65
Guido van Rossumc56ba381997-09-16 18:42:04 +000066class Exception:
Guido van Rossum43389201999-01-15 04:03:46 +000067 """Proposed base class for all exceptions."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +000068 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000069 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +000070
71 def __str__(self):
Guido van Rossumc56ba381997-09-16 18:42:04 +000072 if not self.args:
Barry Warsaw3e613ce1997-08-29 21:59:26 +000073 return ''
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000074 elif len(self.args) == 1:
75 return str(self.args[0])
76 else:
77 return str(self.args)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000078
79 def __getitem__(self, i):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000080 return self.args[i]
Barry Warsaw3e613ce1997-08-29 21:59:26 +000081
Fred Drake596db311997-10-06 15:48:20 +000082class StandardError(Exception):
Guido van Rossum43389201999-01-15 04:03:46 +000083 """Base class for all standard Python exceptions."""
Fred Drake596db311997-10-06 15:48:20 +000084 pass
85
Barry Warsaw3e613ce1997-08-29 21:59:26 +000086class SyntaxError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +000087 """Invalid syntax."""
Guido van Rossumf394f561997-09-05 19:00:56 +000088 filename = lineno = offset = text = None
Guido van Rossumc56ba381997-09-16 18:42:04 +000089 msg = ""
90 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000091 self.args = args
92 if len(self.args) >= 1:
93 self.msg = self.args[0]
94 if len(self.args) == 2:
95 info = self.args[1]
96 try:
97 self.filename, self.lineno, self.offset, self.text = info
98 except:
99 pass
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000100 def __str__(self):
Guido van Rossumf394f561997-09-05 19:00:56 +0000101 return str(self.msg)
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000102
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000103class EnvironmentError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000104 """Base class for I/O related errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000105 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000106 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000107 self.errno = None
108 self.strerror = None
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000109 self.filename = None
110 if len(args) == 3:
111 # open() errors give third argument which is the filename. BUT,
112 # so common in-place unpacking doesn't break, e.g.:
113 #
114 # except IOError, (errno, strerror):
115 #
116 # we hack args so that it only contains two items. This also
117 # means we need our own __str__() which prints out the filename
118 # when it was supplied.
119 self.errno, self.strerror, self.filename = args
120 self.args = args[0:2]
Guido van Rossumc56ba381997-09-16 18:42:04 +0000121 if len(args) == 2:
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000122 # common case: PyErr_SetFromErrno()
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000123 self.errno, self.strerror = args
124
125 def __str__(self):
Guido van Rossumbe21d981998-08-11 18:01:32 +0000126 if self.filename is not None:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000127 return '[Errno %s] %s: %s' % (self.errno, self.strerror,
Guido van Rossumbe21d981998-08-11 18:01:32 +0000128 repr(self.filename))
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000129 elif self.errno and self.strerror:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000130 return '[Errno %s] %s' % (self.errno, self.strerror)
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000131 else:
132 return StandardError.__str__(self)
133
134class IOError(EnvironmentError):
Guido van Rossum43389201999-01-15 04:03:46 +0000135 """I/O operation failed."""
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000136 pass
137
138class OSError(EnvironmentError):
Guido van Rossum43389201999-01-15 04:03:46 +0000139 """OS system call failed."""
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000140 pass
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000141
142class RuntimeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000143 """Unspecified run-time error."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000144 pass
145
Barry Warsaw8fe2a341998-12-01 18:36:30 +0000146class NotImplementedError(RuntimeError):
Guido van Rossum43389201999-01-15 04:03:46 +0000147 """Method or function hasn't been implemented yet."""
Barry Warsaw8fe2a341998-12-01 18:36:30 +0000148 pass
149
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000150class SystemError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000151 """Internal error in the Python interpreter.
152
153 Please report this to the Python maintainer, along with the traceback,
154 the Python version, and the hardware/OS platform and version."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000155 pass
156
157class EOFError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000158 """Read beyond end of file."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000159 pass
160
161class ImportError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000162 """Import can't find module, or can't find name in module."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000163 pass
164
165class TypeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000166 """Inappropriate argument type."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000167 pass
168
169class ValueError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000170 """Inappropriate argument value (of correct type)."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000171 pass
172
173class KeyboardInterrupt(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000174 """Program interrupted by user."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000175 pass
176
177class AssertionError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000178 """Assertion failed."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000179 pass
180
Barry Warsaw25131fa1997-09-16 21:50:59 +0000181class ArithmeticError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000182 """Base class for arithmetic errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000183 pass
184
Barry Warsaw25131fa1997-09-16 21:50:59 +0000185class OverflowError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000186 """Result too large to be represented."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000187 pass
188
Barry Warsaw25131fa1997-09-16 21:50:59 +0000189class FloatingPointError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000190 """Floating point operation failed."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000191 pass
192
Barry Warsaw25131fa1997-09-16 21:50:59 +0000193class ZeroDivisionError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000194 """Second argument to a division or modulo operation was zero."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000195 pass
196
197class LookupError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000198 """Base class for lookup errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000199 pass
200
201class IndexError(LookupError):
Guido van Rossum43389201999-01-15 04:03:46 +0000202 """Sequence index out of range."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000203 pass
204
205class KeyError(LookupError):
Guido van Rossum43389201999-01-15 04:03:46 +0000206 """Mapping key not found."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000207 pass
208
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000209class AttributeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000210 """Attribute not found."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000211 pass
212
213class NameError(StandardError):
Guido van Rossum87460821999-06-22 14:47:32 +0000214 """Name not found globally."""
215 pass
216
217class UnboundLocalError(NameError):
218 """Local name referenced but not bound to a value."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000219 pass
220
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000221class MemoryError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000222 """Out of memory."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000223 pass
Guido van Rossumc56ba381997-09-16 18:42:04 +0000224
225class SystemExit(Exception):
Guido van Rossum43389201999-01-15 04:03:46 +0000226 """Request to exit from the interpreter."""
Guido van Rossumc56ba381997-09-16 18:42:04 +0000227 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000228 self.args = args
Guido van Rossumc56ba381997-09-16 18:42:04 +0000229 if len(args) == 0:
230 self.code = None
231 elif len(args) == 1:
232 self.code = args[0]
233 else:
234 self.code = args