blob: e943f7b18830a665a2893ed6899217127bc3f2fb [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
43 +-- AttributeError
44 +-- SyntaxError
45 +-- TypeError
46 +-- AssertionError
47 +-- LookupError(*)
48 | |
49 | +-- IndexError
50 | +-- KeyError
51 |
Barry Warsaw25131fa1997-09-16 21:50:59 +000052 +-- ArithmeticError(*)
Guido van Rossumc56ba381997-09-16 18:42:04 +000053 | |
54 | +-- OverflowError
55 | +-- ZeroDivisionError
56 | +-- FloatingPointError
57 |
58 +-- ValueError
59 +-- SystemError
60 +-- MemoryError
Barry Warsaw3e613ce1997-08-29 21:59:26 +000061"""
62
Guido van Rossumc56ba381997-09-16 18:42:04 +000063class Exception:
Guido van Rossum43389201999-01-15 04:03:46 +000064 """Proposed base class for all exceptions."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +000065 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000066 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +000067
68 def __str__(self):
Guido van Rossumc56ba381997-09-16 18:42:04 +000069 if not self.args:
Barry Warsaw3e613ce1997-08-29 21:59:26 +000070 return ''
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000071 elif len(self.args) == 1:
72 return str(self.args[0])
73 else:
74 return str(self.args)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000075
76 def __getitem__(self, i):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000077 return self.args[i]
Barry Warsaw3e613ce1997-08-29 21:59:26 +000078
Fred Drake596db311997-10-06 15:48:20 +000079class StandardError(Exception):
Guido van Rossum43389201999-01-15 04:03:46 +000080 """Base class for all standard Python exceptions."""
Fred Drake596db311997-10-06 15:48:20 +000081 pass
82
Barry Warsaw3e613ce1997-08-29 21:59:26 +000083class SyntaxError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +000084 """Invalid syntax."""
Guido van Rossumf394f561997-09-05 19:00:56 +000085 filename = lineno = offset = text = None
Guido van Rossumc56ba381997-09-16 18:42:04 +000086 msg = ""
87 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000088 self.args = args
89 if len(self.args) >= 1:
90 self.msg = self.args[0]
91 if len(self.args) == 2:
92 info = self.args[1]
93 try:
94 self.filename, self.lineno, self.offset, self.text = info
95 except:
96 pass
Barry Warsaw3e613ce1997-08-29 21:59:26 +000097 def __str__(self):
Guido van Rossumf394f561997-09-05 19:00:56 +000098 return str(self.msg)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000099
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000100class EnvironmentError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000101 """Base class for I/O related errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000102 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000103 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000104 self.errno = None
105 self.strerror = None
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000106 self.filename = None
107 if len(args) == 3:
108 # open() errors give third argument which is the filename. BUT,
109 # so common in-place unpacking doesn't break, e.g.:
110 #
111 # except IOError, (errno, strerror):
112 #
113 # we hack args so that it only contains two items. This also
114 # means we need our own __str__() which prints out the filename
115 # when it was supplied.
116 self.errno, self.strerror, self.filename = args
117 self.args = args[0:2]
Guido van Rossumc56ba381997-09-16 18:42:04 +0000118 if len(args) == 2:
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000119 # common case: PyErr_SetFromErrno()
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000120 self.errno, self.strerror = args
121
122 def __str__(self):
Guido van Rossumbe21d981998-08-11 18:01:32 +0000123 if self.filename is not None:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000124 return '[Errno %s] %s: %s' % (self.errno, self.strerror,
Guido van Rossumbe21d981998-08-11 18:01:32 +0000125 repr(self.filename))
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000126 elif self.errno and self.strerror:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000127 return '[Errno %s] %s' % (self.errno, self.strerror)
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000128 else:
129 return StandardError.__str__(self)
130
131class IOError(EnvironmentError):
Guido van Rossum43389201999-01-15 04:03:46 +0000132 """I/O operation failed."""
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000133 pass
134
135class OSError(EnvironmentError):
Guido van Rossum43389201999-01-15 04:03:46 +0000136 """OS system call failed."""
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000137 pass
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000138
139class RuntimeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000140 """Unspecified run-time error."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000141 pass
142
Barry Warsaw8fe2a341998-12-01 18:36:30 +0000143class NotImplementedError(RuntimeError):
Guido van Rossum43389201999-01-15 04:03:46 +0000144 """Method or function hasn't been implemented yet."""
Barry Warsaw8fe2a341998-12-01 18:36:30 +0000145 pass
146
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000147class SystemError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000148 """Internal error in the Python interpreter.
149
150 Please report this to the Python maintainer, along with the traceback,
151 the Python version, and the hardware/OS platform and version."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000152 pass
153
154class EOFError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000155 """Read beyond end of file."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000156 pass
157
158class ImportError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000159 """Import can't find module, or can't find name in module."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000160 pass
161
162class TypeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000163 """Inappropriate argument type."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000164 pass
165
166class ValueError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000167 """Inappropriate argument value (of correct type)."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000168 pass
169
170class KeyboardInterrupt(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000171 """Program interrupted by user."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000172 pass
173
174class AssertionError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000175 """Assertion failed."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000176 pass
177
Barry Warsaw25131fa1997-09-16 21:50:59 +0000178class ArithmeticError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000179 """Base class for arithmetic errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000180 pass
181
Barry Warsaw25131fa1997-09-16 21:50:59 +0000182class OverflowError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000183 """Result too large to be represented."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000184 pass
185
Barry Warsaw25131fa1997-09-16 21:50:59 +0000186class FloatingPointError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000187 """Floating point operation failed."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000188 pass
189
Barry Warsaw25131fa1997-09-16 21:50:59 +0000190class ZeroDivisionError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000191 """Second argument to a division or modulo operation was zero."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000192 pass
193
194class LookupError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000195 """Base class for lookup errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000196 pass
197
198class IndexError(LookupError):
Guido van Rossum43389201999-01-15 04:03:46 +0000199 """Sequence index out of range."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000200 pass
201
202class KeyError(LookupError):
Guido van Rossum43389201999-01-15 04:03:46 +0000203 """Mapping key not found."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000204 pass
205
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000206class AttributeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000207 """Attribute not found."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000208 pass
209
210class NameError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000211 """Name not found locally or globally."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000212 pass
213
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000214class MemoryError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000215 """Out of memory."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000216 pass
Guido van Rossumc56ba381997-09-16 18:42:04 +0000217
218class SystemExit(Exception):
Guido van Rossum43389201999-01-15 04:03:46 +0000219 """Request to exit from the interpreter."""
Guido van Rossumc56ba381997-09-16 18:42:04 +0000220 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000221 self.args = args
Guido van Rossumc56ba381997-09-16 18:42:04 +0000222 if len(args) == 0:
223 self.code = None
224 elif len(args) == 1:
225 self.code = args[0]
226 else:
227 self.code = args