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 |
Barry Warsaw | 40db48c | 1999-02-24 00:27:39 +0000 | [diff] [blame] | 14 | file, but it isn't recommended because the old string based exceptions won't |
| 15 | be kept in sync. The class names described here are expected to be found by |
| 16 | the bltinmodule.c file. If you add classes here, you must modify |
| 17 | bltinmodule.c or the exceptions won't be available in the __builtin__ module, |
| 18 | nor will they be accessible from C. |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 19 | |
Barry Warsaw | 8fe2a34 | 1998-12-01 18:36:30 +0000 | [diff] [blame] | 20 | 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] | 21 | containing the derived exceptions when string-based exceptions are used. If |
| 22 | you define your own class based exceptions, they should be derived from |
| 23 | Exception. |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 25 | Exception(*) |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 26 | | |
Barry Warsaw | 40db48c | 1999-02-24 00:27:39 +0000 | [diff] [blame] | 27 | +-- SystemExit |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 28 | +-- StandardError(*) |
| 29 | | |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 30 | +-- KeyboardInterrupt |
| 31 | +-- ImportError |
Barry Warsaw | 9195f55 | 1998-09-25 22:43:21 +0000 | [diff] [blame] | 32 | +-- EnvironmentError(*) |
| 33 | | | |
| 34 | | +-- IOError |
| 35 | | +-- OSError(*) |
| 36 | | |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 37 | +-- EOFError |
| 38 | +-- RuntimeError |
Barry Warsaw | 8fe2a34 | 1998-12-01 18:36:30 +0000 | [diff] [blame] | 39 | | | |
| 40 | | +-- NotImplementedError(*) |
| 41 | | |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 42 | +-- NameError |
Guido van Rossum | 8746082 | 1999-06-22 14:47:32 +0000 | [diff] [blame] | 43 | | | |
| 44 | | +-- UnboundLocalError(*) |
Barry Warsaw | 008edbf | 1999-08-19 21:17:08 +0000 | [diff] [blame] | 45 | | |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 46 | +-- AttributeError |
| 47 | +-- SyntaxError |
| 48 | +-- TypeError |
| 49 | +-- AssertionError |
| 50 | +-- LookupError(*) |
| 51 | | | |
| 52 | | +-- IndexError |
| 53 | | +-- KeyError |
| 54 | | |
Barry Warsaw | 25131fa | 1997-09-16 21:50:59 +0000 | [diff] [blame] | 55 | +-- ArithmeticError(*) |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 56 | | | |
| 57 | | +-- OverflowError |
| 58 | | +-- ZeroDivisionError |
| 59 | | +-- FloatingPointError |
| 60 | | |
| 61 | +-- ValueError |
| 62 | +-- SystemError |
| 63 | +-- MemoryError |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 64 | """ |
| 65 | |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 66 | class Exception: |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 67 | """Proposed base class for all exceptions.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 68 | def __init__(self, *args): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 69 | self.args = args |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 70 | |
| 71 | def __str__(self): |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 72 | if not self.args: |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 73 | return '' |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 74 | elif len(self.args) == 1: |
| 75 | return str(self.args[0]) |
| 76 | else: |
| 77 | return str(self.args) |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 78 | |
| 79 | def __getitem__(self, i): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 80 | return self.args[i] |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 81 | |
Fred Drake | 596db31 | 1997-10-06 15:48:20 +0000 | [diff] [blame] | 82 | class StandardError(Exception): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 83 | """Base class for all standard Python exceptions.""" |
Fred Drake | 596db31 | 1997-10-06 15:48:20 +0000 | [diff] [blame] | 84 | pass |
| 85 | |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 86 | class SyntaxError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 87 | """Invalid syntax.""" |
Guido van Rossum | f394f56 | 1997-09-05 19:00:56 +0000 | [diff] [blame] | 88 | filename = lineno = offset = text = None |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 89 | msg = "" |
| 90 | def __init__(self, *args): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 91 | 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 Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 100 | def __str__(self): |
Guido van Rossum | f394f56 | 1997-09-05 19:00:56 +0000 | [diff] [blame] | 101 | return str(self.msg) |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 102 | |
Barry Warsaw | 2dfe4de | 1998-07-23 16:03:46 +0000 | [diff] [blame] | 103 | class EnvironmentError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 104 | """Base class for I/O related errors.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 105 | def __init__(self, *args): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 106 | self.args = args |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 107 | self.errno = None |
| 108 | self.strerror = None |
Barry Warsaw | 2dfe4de | 1998-07-23 16:03:46 +0000 | [diff] [blame] | 109 | 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 Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 121 | if len(args) == 2: |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 122 | # common case: PyErr_SetFromErrno() |
Barry Warsaw | 2dfe4de | 1998-07-23 16:03:46 +0000 | [diff] [blame] | 123 | self.errno, self.strerror = args |
| 124 | |
| 125 | def __str__(self): |
Guido van Rossum | be21d98 | 1998-08-11 18:01:32 +0000 | [diff] [blame] | 126 | if self.filename is not None: |
Jeremy Hylton | ec8c8c2 | 1998-07-28 17:30:06 +0000 | [diff] [blame] | 127 | return '[Errno %s] %s: %s' % (self.errno, self.strerror, |
Guido van Rossum | be21d98 | 1998-08-11 18:01:32 +0000 | [diff] [blame] | 128 | repr(self.filename)) |
Barry Warsaw | 2dfe4de | 1998-07-23 16:03:46 +0000 | [diff] [blame] | 129 | elif self.errno and self.strerror: |
Jeremy Hylton | ec8c8c2 | 1998-07-28 17:30:06 +0000 | [diff] [blame] | 130 | return '[Errno %s] %s' % (self.errno, self.strerror) |
Barry Warsaw | 2dfe4de | 1998-07-23 16:03:46 +0000 | [diff] [blame] | 131 | else: |
| 132 | return StandardError.__str__(self) |
| 133 | |
| 134 | class IOError(EnvironmentError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 135 | """I/O operation failed.""" |
Barry Warsaw | 2dfe4de | 1998-07-23 16:03:46 +0000 | [diff] [blame] | 136 | pass |
| 137 | |
| 138 | class OSError(EnvironmentError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 139 | """OS system call failed.""" |
Barry Warsaw | 2dfe4de | 1998-07-23 16:03:46 +0000 | [diff] [blame] | 140 | pass |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 141 | |
| 142 | class RuntimeError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 143 | """Unspecified run-time error.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 144 | pass |
| 145 | |
Barry Warsaw | 8fe2a34 | 1998-12-01 18:36:30 +0000 | [diff] [blame] | 146 | class NotImplementedError(RuntimeError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 147 | """Method or function hasn't been implemented yet.""" |
Barry Warsaw | 8fe2a34 | 1998-12-01 18:36:30 +0000 | [diff] [blame] | 148 | pass |
| 149 | |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 150 | class SystemError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 151 | """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 Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 155 | pass |
| 156 | |
| 157 | class EOFError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 158 | """Read beyond end of file.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 159 | pass |
| 160 | |
| 161 | class ImportError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 162 | """Import can't find module, or can't find name in module.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 163 | pass |
| 164 | |
| 165 | class TypeError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 166 | """Inappropriate argument type.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 167 | pass |
| 168 | |
| 169 | class ValueError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 170 | """Inappropriate argument value (of correct type).""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 171 | pass |
| 172 | |
| 173 | class KeyboardInterrupt(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 174 | """Program interrupted by user.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 175 | pass |
| 176 | |
| 177 | class AssertionError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 178 | """Assertion failed.""" |
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 ArithmeticError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 182 | """Base class for arithmetic errors.""" |
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 OverflowError(ArithmeticError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 186 | """Result too large to be represented.""" |
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 FloatingPointError(ArithmeticError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 190 | """Floating point operation failed.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 191 | pass |
| 192 | |
Barry Warsaw | 25131fa | 1997-09-16 21:50:59 +0000 | [diff] [blame] | 193 | class ZeroDivisionError(ArithmeticError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 194 | """Second argument to a division or modulo operation was zero.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 195 | pass |
| 196 | |
| 197 | class LookupError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 198 | """Base class for lookup errors.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 199 | pass |
| 200 | |
| 201 | class IndexError(LookupError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 202 | """Sequence index out of range.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 203 | pass |
| 204 | |
| 205 | class KeyError(LookupError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 206 | """Mapping key not found.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 207 | pass |
| 208 | |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 209 | class AttributeError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 210 | """Attribute not found.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 211 | pass |
| 212 | |
| 213 | class NameError(StandardError): |
Guido van Rossum | 8746082 | 1999-06-22 14:47:32 +0000 | [diff] [blame] | 214 | """Name not found globally.""" |
| 215 | pass |
| 216 | |
| 217 | class UnboundLocalError(NameError): |
| 218 | """Local name referenced but not bound to a value.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 219 | pass |
| 220 | |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 221 | class MemoryError(StandardError): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 222 | """Out of memory.""" |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 223 | pass |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 224 | |
| 225 | class SystemExit(Exception): |
Guido van Rossum | 4338920 | 1999-01-15 04:03:46 +0000 | [diff] [blame] | 226 | """Request to exit from the interpreter.""" |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 227 | def __init__(self, *args): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 228 | self.args = args |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 229 | if len(args) == 0: |
| 230 | self.code = None |
| 231 | elif len(args) == 1: |
| 232 | self.code = args[0] |
| 233 | else: |
| 234 | self.code = args |