blob: 2963c04c708034e48f053e86bd9c65094d4398bd [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
14file, but it isn't recommended. The class names described here are expected
Barry Warsaw8fe2a341998-12-01 18:36:30 +000015to be found by the bltinmodule.c file. If you add classes here, you must
16modify bltinmodule.c or the exceptions won't be available in the __builtin__
17module, nor will they be accessible from C.
Barry Warsaw3e613ce1997-08-29 21:59:26 +000018
Barry Warsaw8fe2a341998-12-01 18:36:30 +000019The classes with a `*' are new since Python 1.5. They are defined as tuples
Barry Warsaw9195f551998-09-25 22:43:21 +000020containing the derived exceptions when string-based exceptions are used. If
21you define your own class based exceptions, they should be derived from
22Exception.
Barry Warsaw3e613ce1997-08-29 21:59:26 +000023
Guido van Rossumc56ba381997-09-16 18:42:04 +000024Exception(*)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000025 |
Guido van Rossumc56ba381997-09-16 18:42:04 +000026 +-- StandardError(*)
27 |
28 +-- SystemExit
29 +-- KeyboardInterrupt
30 +-- ImportError
Barry Warsaw9195f551998-09-25 22:43:21 +000031 +-- EnvironmentError(*)
32 | |
33 | +-- IOError
34 | +-- OSError(*)
35 |
Guido van Rossumc56ba381997-09-16 18:42:04 +000036 +-- EOFError
37 +-- RuntimeError
Barry Warsaw8fe2a341998-12-01 18:36:30 +000038 | |
39 | +-- NotImplementedError(*)
40 |
Guido van Rossumc56ba381997-09-16 18:42:04 +000041 +-- NameError
42 +-- AttributeError
43 +-- SyntaxError
44 +-- TypeError
45 +-- AssertionError
46 +-- LookupError(*)
47 | |
48 | +-- IndexError
49 | +-- KeyError
50 |
Barry Warsaw25131fa1997-09-16 21:50:59 +000051 +-- ArithmeticError(*)
Guido van Rossumc56ba381997-09-16 18:42:04 +000052 | |
53 | +-- OverflowError
54 | +-- ZeroDivisionError
55 | +-- FloatingPointError
56 |
57 +-- ValueError
58 +-- SystemError
59 +-- MemoryError
Barry Warsaw3e613ce1997-08-29 21:59:26 +000060"""
61
Guido van Rossumc56ba381997-09-16 18:42:04 +000062class Exception:
Guido van Rossum43389201999-01-15 04:03:46 +000063 """Proposed base class for all exceptions."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +000064 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000065 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +000066
67 def __str__(self):
Guido van Rossumc56ba381997-09-16 18:42:04 +000068 if not self.args:
Barry Warsaw3e613ce1997-08-29 21:59:26 +000069 return ''
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000070 elif len(self.args) == 1:
71 return str(self.args[0])
72 else:
73 return str(self.args)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000074
75 def __getitem__(self, i):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000076 return self.args[i]
Barry Warsaw3e613ce1997-08-29 21:59:26 +000077
Fred Drake596db311997-10-06 15:48:20 +000078class StandardError(Exception):
Guido van Rossum43389201999-01-15 04:03:46 +000079 """Base class for all standard Python exceptions."""
Fred Drake596db311997-10-06 15:48:20 +000080 pass
81
Barry Warsaw3e613ce1997-08-29 21:59:26 +000082class SyntaxError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +000083 """Invalid syntax."""
Guido van Rossumf394f561997-09-05 19:00:56 +000084 filename = lineno = offset = text = None
Guido van Rossumc56ba381997-09-16 18:42:04 +000085 msg = ""
86 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000087 self.args = args
88 if len(self.args) >= 1:
89 self.msg = self.args[0]
90 if len(self.args) == 2:
91 info = self.args[1]
92 try:
93 self.filename, self.lineno, self.offset, self.text = info
94 except:
95 pass
Barry Warsaw3e613ce1997-08-29 21:59:26 +000096 def __str__(self):
Guido van Rossumf394f561997-09-05 19:00:56 +000097 return str(self.msg)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000098
Barry Warsaw2dfe4de1998-07-23 16:03:46 +000099class EnvironmentError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000100 """Base class for I/O related errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000101 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000102 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000103 self.errno = None
104 self.strerror = None
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000105 self.filename = None
106 if len(args) == 3:
107 # open() errors give third argument which is the filename. BUT,
108 # so common in-place unpacking doesn't break, e.g.:
109 #
110 # except IOError, (errno, strerror):
111 #
112 # we hack args so that it only contains two items. This also
113 # means we need our own __str__() which prints out the filename
114 # when it was supplied.
115 self.errno, self.strerror, self.filename = args
116 self.args = args[0:2]
Guido van Rossumc56ba381997-09-16 18:42:04 +0000117 if len(args) == 2:
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000118 # common case: PyErr_SetFromErrno()
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000119 self.errno, self.strerror = args
120
121 def __str__(self):
Guido van Rossumbe21d981998-08-11 18:01:32 +0000122 if self.filename is not None:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000123 return '[Errno %s] %s: %s' % (self.errno, self.strerror,
Guido van Rossumbe21d981998-08-11 18:01:32 +0000124 repr(self.filename))
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000125 elif self.errno and self.strerror:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000126 return '[Errno %s] %s' % (self.errno, self.strerror)
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000127 else:
128 return StandardError.__str__(self)
129
130class IOError(EnvironmentError):
Guido van Rossum43389201999-01-15 04:03:46 +0000131 """I/O operation failed."""
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000132 pass
133
134class OSError(EnvironmentError):
Guido van Rossum43389201999-01-15 04:03:46 +0000135 """OS system call failed."""
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000136 pass
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000137
138class RuntimeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000139 """Unspecified run-time error."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000140 pass
141
Barry Warsaw8fe2a341998-12-01 18:36:30 +0000142class NotImplementedError(RuntimeError):
Guido van Rossum43389201999-01-15 04:03:46 +0000143 """Method or function hasn't been implemented yet."""
Barry Warsaw8fe2a341998-12-01 18:36:30 +0000144 pass
145
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000146class SystemError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000147 """Internal error in the Python interpreter.
148
149 Please report this to the Python maintainer, along with the traceback,
150 the Python version, and the hardware/OS platform and version."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000151 pass
152
153class EOFError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000154 """Read beyond end of file."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000155 pass
156
157class ImportError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000158 """Import can't find module, or can't find name in module."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000159 pass
160
161class TypeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000162 """Inappropriate argument type."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000163 pass
164
165class ValueError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000166 """Inappropriate argument value (of correct type)."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000167 pass
168
169class KeyboardInterrupt(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000170 """Program interrupted by user."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000171 pass
172
173class AssertionError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000174 """Assertion failed."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000175 pass
176
Barry Warsaw25131fa1997-09-16 21:50:59 +0000177class ArithmeticError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000178 """Base class for arithmetic errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000179 pass
180
Barry Warsaw25131fa1997-09-16 21:50:59 +0000181class OverflowError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000182 """Result too large to be represented."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000183 pass
184
Barry Warsaw25131fa1997-09-16 21:50:59 +0000185class FloatingPointError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000186 """Floating point operation failed."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000187 pass
188
Barry Warsaw25131fa1997-09-16 21:50:59 +0000189class ZeroDivisionError(ArithmeticError):
Guido van Rossum43389201999-01-15 04:03:46 +0000190 """Second argument to a division or modulo operation was zero."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000191 pass
192
193class LookupError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000194 """Base class for lookup errors."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000195 pass
196
197class IndexError(LookupError):
Guido van Rossum43389201999-01-15 04:03:46 +0000198 """Sequence index out of range."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000199 pass
200
201class KeyError(LookupError):
Guido van Rossum43389201999-01-15 04:03:46 +0000202 """Mapping key not found."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000203 pass
204
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000205class AttributeError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000206 """Attribute not found."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000207 pass
208
209class NameError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000210 """Name not found locally or globally."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000211 pass
212
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000213class MemoryError(StandardError):
Guido van Rossum43389201999-01-15 04:03:46 +0000214 """Out of memory."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000215 pass
Guido van Rossumc56ba381997-09-16 18:42:04 +0000216
217class SystemExit(Exception):
Guido van Rossum43389201999-01-15 04:03:46 +0000218 """Request to exit from the interpreter."""
Guido van Rossumc56ba381997-09-16 18:42:04 +0000219 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000220 self.args = args
Guido van Rossumc56ba381997-09-16 18:42:04 +0000221 if len(args) == 0:
222 self.code = None
223 elif len(args) == 1:
224 self.code = args[0]
225 else:
226 self.code = args