Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 1 | """Class based built-in exception hierarchy. |
| 2 | |
Barry Warsaw | 9195f55 | 1998-09-25 22:43:21 +0000 | [diff] [blame] | 3 | New with Python 1.5, all standard built-in exceptions are now class objects by |
| 4 | default. This gives Python's exception handling mechanism a more |
| 5 | object-oriented feel. Traditionally they were string objects. Python will |
| 6 | fallback to string based exceptions if the interpreter is invoked with the -X |
| 7 | option, or if some failure occurs during class exception initialization (in |
| 8 | this case a warning will be printed). |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 9 | |
Barry Warsaw | 9195f55 | 1998-09-25 22:43:21 +0000 | [diff] [blame] | 10 | Most existing code should continue to work with class based exceptions. Some |
| 11 | tricky uses of IOError may break, but the most common uses should work. |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 12 | |
Barry Warsaw | 9195f55 | 1998-09-25 22:43:21 +0000 | [diff] [blame] | 13 | Here is a rundown of the class hierarchy. You can change this by editing this |
| 14 | file, but it isn't recommended. The class names described here are expected |
Barry Warsaw | 8fe2a34 | 1998-12-01 18:36:30 +0000 | [diff] [blame] | 15 | to be found by the bltinmodule.c file. If you add classes here, you must |
| 16 | modify bltinmodule.c or the exceptions won't be available in the __builtin__ |
| 17 | module, nor will they be accessible from C. |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 18 | |
Barry Warsaw | 8fe2a34 | 1998-12-01 18:36:30 +0000 | [diff] [blame] | 19 | The classes with a `*' are new since Python 1.5. They are defined as tuples |
Barry Warsaw | 9195f55 | 1998-09-25 22:43:21 +0000 | [diff] [blame] | 20 | containing the derived exceptions when string-based exceptions are used. If |
| 21 | you define your own class based exceptions, they should be derived from |
| 22 | Exception. |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 24 | Exception(*) |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 25 | | |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 26 | +-- StandardError(*) |
| 27 | | |
| 28 | +-- SystemExit |
| 29 | +-- KeyboardInterrupt |
| 30 | +-- ImportError |
Barry Warsaw | 9195f55 | 1998-09-25 22:43:21 +0000 | [diff] [blame] | 31 | +-- EnvironmentError(*) |
| 32 | | | |
| 33 | | +-- IOError |
| 34 | | +-- OSError(*) |
| 35 | | |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 36 | +-- EOFError |
| 37 | +-- RuntimeError |
Barry Warsaw | 8fe2a34 | 1998-12-01 18:36:30 +0000 | [diff] [blame] | 38 | | | |
| 39 | | +-- NotImplementedError(*) |
| 40 | | |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 41 | +-- NameError |
| 42 | +-- AttributeError |
| 43 | +-- SyntaxError |
| 44 | +-- TypeError |
| 45 | +-- AssertionError |
| 46 | +-- LookupError(*) |
| 47 | | | |
| 48 | | +-- IndexError |
| 49 | | +-- KeyError |
| 50 | | |
Barry Warsaw | 25131fa | 1997-09-16 21:50:59 +0000 | [diff] [blame] | 51 | +-- ArithmeticError(*) |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 52 | | | |
| 53 | | +-- OverflowError |
| 54 | | +-- ZeroDivisionError |
| 55 | | +-- FloatingPointError |
| 56 | | |
| 57 | +-- ValueError |
| 58 | +-- SystemError |
| 59 | +-- MemoryError |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 60 | """ |
| 61 | |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 62 | class Exception: |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 63 | """Proposed base class for all exceptions.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 64 | def __init__(self, *args): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 65 | self.args = args |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 66 | |
| 67 | def __str__(self): |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 68 | if not self.args: |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 69 | return '' |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 70 | elif len(self.args) == 1: |
| 71 | return str(self.args[0]) |
| 72 | else: |
| 73 | return str(self.args) |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 74 | |
| 75 | def __getitem__(self, i): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 76 | return self.args[i] |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 77 | |
Fred Drake | 596db31 | 1997-10-06 15:48:20 +0000 | [diff] [blame] | 78 | class StandardError(Exception): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 79 | """Base class for all standard Python exceptions.""" |
Fred Drake | 596db31 | 1997-10-06 15:48:20 +0000 | [diff] [blame] | 80 | pass |
| 81 | |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 82 | class SyntaxError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 83 | """Invalid syntax.""" |
Guido van Rossum | f394f56 | 1997-09-05 19:00:56 +0000 | [diff] [blame] | 84 | filename = lineno = offset = text = None |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 85 | msg = "" |
| 86 | def __init__(self, *args): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 87 | 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 Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 96 | def __str__(self): |
Guido van Rossum | f394f56 | 1997-09-05 19:00:56 +0000 | [diff] [blame] | 97 | return str(self.msg) |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 98 | |
Barry Warsaw | 2dfe4de | 1998-07-23 16:03:46 +0000 | [diff] [blame] | 99 | class EnvironmentError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 100 | """Base class for I/O related errors.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 101 | def __init__(self, *args): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 102 | self.args = args |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 103 | self.errno = None |
| 104 | self.strerror = None |
Barry Warsaw | 2dfe4de | 1998-07-23 16:03:46 +0000 | [diff] [blame] | 105 | 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 Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 117 | if len(args) == 2: |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 118 | # common case: PyErr_SetFromErrno() |
Barry Warsaw | 2dfe4de | 1998-07-23 16:03:46 +0000 | [diff] [blame] | 119 | self.errno, self.strerror = args |
| 120 | |
| 121 | def __str__(self): |
Guido van Rossum | be21d98 | 1998-08-11 18:01:32 +0000 | [diff] [blame] | 122 | if self.filename is not None: |
Jeremy Hylton | ec8c8c2 | 1998-07-28 17:30:06 +0000 | [diff] [blame] | 123 | return '[Errno %s] %s: %s' % (self.errno, self.strerror, |
Guido van Rossum | be21d98 | 1998-08-11 18:01:32 +0000 | [diff] [blame] | 124 | repr(self.filename)) |
Barry Warsaw | 2dfe4de | 1998-07-23 16:03:46 +0000 | [diff] [blame] | 125 | elif self.errno and self.strerror: |
Jeremy Hylton | ec8c8c2 | 1998-07-28 17:30:06 +0000 | [diff] [blame] | 126 | return '[Errno %s] %s' % (self.errno, self.strerror) |
Barry Warsaw | 2dfe4de | 1998-07-23 16:03:46 +0000 | [diff] [blame] | 127 | else: |
| 128 | return StandardError.__str__(self) |
| 129 | |
| 130 | class IOError(EnvironmentError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 131 | """I/O operation failed.""" |
Barry Warsaw | 2dfe4de | 1998-07-23 16:03:46 +0000 | [diff] [blame] | 132 | pass |
| 133 | |
| 134 | class OSError(EnvironmentError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 135 | """OS system call failed.""" |
Barry Warsaw | 2dfe4de | 1998-07-23 16:03:46 +0000 | [diff] [blame] | 136 | pass |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 137 | |
| 138 | class RuntimeError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 139 | """Unspecified run-time error.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 140 | pass |
| 141 | |
Barry Warsaw | 8fe2a34 | 1998-12-01 18:36:30 +0000 | [diff] [blame] | 142 | class NotImplementedError(RuntimeError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 143 | """Method or function hasn't been implemented yet.""" |
Barry Warsaw | 8fe2a34 | 1998-12-01 18:36:30 +0000 | [diff] [blame] | 144 | pass |
| 145 | |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 146 | class SystemError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 147 | """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 Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 151 | pass |
| 152 | |
| 153 | class EOFError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 154 | """Read beyond end of file.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 155 | pass |
| 156 | |
| 157 | class ImportError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 158 | """Import can't find module, or can't find name in module.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 159 | pass |
| 160 | |
| 161 | class TypeError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 162 | """Inappropriate argument type.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 163 | pass |
| 164 | |
| 165 | class ValueError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 166 | """Inappropriate argument value (of correct type).""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 167 | pass |
| 168 | |
| 169 | class KeyboardInterrupt(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 170 | """Program interrupted by user.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 171 | pass |
| 172 | |
| 173 | class AssertionError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 174 | """Assertion failed.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 175 | pass |
| 176 | |
Barry Warsaw | 25131fa | 1997-09-16 21:50:59 +0000 | [diff] [blame] | 177 | class ArithmeticError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 178 | """Base class for arithmetic errors.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 179 | pass |
| 180 | |
Barry Warsaw | 25131fa | 1997-09-16 21:50:59 +0000 | [diff] [blame] | 181 | class OverflowError(ArithmeticError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 182 | """Result too large to be represented.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 183 | pass |
| 184 | |
Barry Warsaw | 25131fa | 1997-09-16 21:50:59 +0000 | [diff] [blame] | 185 | class FloatingPointError(ArithmeticError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 186 | """Floating point operation failed.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 187 | pass |
| 188 | |
Barry Warsaw | 25131fa | 1997-09-16 21:50:59 +0000 | [diff] [blame] | 189 | class ZeroDivisionError(ArithmeticError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 190 | """Second argument to a division or modulo operation was zero.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 191 | pass |
| 192 | |
| 193 | class LookupError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 194 | """Base class for lookup errors.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 195 | pass |
| 196 | |
| 197 | class IndexError(LookupError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 198 | """Sequence index out of range.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 199 | pass |
| 200 | |
| 201 | class KeyError(LookupError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 202 | """Mapping key not found.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 203 | pass |
| 204 | |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 205 | class AttributeError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 206 | """Attribute not found.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 207 | pass |
| 208 | |
| 209 | class NameError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 210 | """Name not found locally or globally.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 211 | pass |
| 212 | |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 213 | class MemoryError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 214 | """Out of memory.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 215 | pass |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 216 | |
| 217 | class SystemExit(Exception): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 218 | """Request to exit from the interpreter.""" |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 219 | def __init__(self, *args): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 220 | self.args = args |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 221 | if len(args) == 0: |
| 222 | self.code = None |
| 223 | elif len(args) == 1: |
| 224 | self.code = args[0] |
| 225 | else: |
| 226 | self.code = args |