Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 1 | """Class based built-in exception hierarchy. |
| 2 | |
Guido van Rossum | 64e736b | 1998-10-02 01:23:47 +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). |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 64e736b | 1998-10-02 01:23:47 +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. |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 64e736b | 1998-10-02 01:23:47 +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 |
Guido van Rossum | 7ea1d97 | 1998-12-22 13:50:33 +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. |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 7ea1d97 | 1998-12-22 13:50:33 +0000 | [diff] [blame] | 19 | The classes with a `*' are new since Python 1.5. They are defined as tuples |
Guido van Rossum | 64e736b | 1998-10-02 01:23:47 +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. |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 23 | |
| 24 | Exception(*) |
| 25 | | |
| 26 | +-- StandardError(*) |
| 27 | | |
| 28 | +-- SystemExit |
| 29 | +-- KeyboardInterrupt |
| 30 | +-- ImportError |
Guido van Rossum | 64e736b | 1998-10-02 01:23:47 +0000 | [diff] [blame] | 31 | +-- EnvironmentError(*) |
| 32 | | | |
| 33 | | +-- IOError |
| 34 | | +-- OSError(*) |
| 35 | | |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 36 | +-- EOFError |
| 37 | +-- RuntimeError |
Guido van Rossum | 7ea1d97 | 1998-12-22 13:50:33 +0000 | [diff] [blame] | 38 | | | |
| 39 | | +-- NotImplementedError(*) |
| 40 | | |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 41 | +-- NameError |
| 42 | +-- AttributeError |
| 43 | +-- SyntaxError |
| 44 | +-- TypeError |
| 45 | +-- AssertionError |
| 46 | +-- LookupError(*) |
| 47 | | | |
| 48 | | +-- IndexError |
| 49 | | +-- KeyError |
| 50 | | |
| 51 | +-- ArithmeticError(*) |
| 52 | | | |
| 53 | | +-- OverflowError |
| 54 | | +-- ZeroDivisionError |
| 55 | | +-- FloatingPointError |
| 56 | | |
| 57 | +-- ValueError |
| 58 | +-- SystemError |
| 59 | +-- MemoryError |
| 60 | """ |
| 61 | |
| 62 | class Exception: |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 63 | """Proposed base class for all exceptions.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 64 | def __init__(self, *args): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 65 | self.args = args |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 66 | |
| 67 | def __str__(self): |
| 68 | if not self.args: |
| 69 | return '' |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 70 | elif len(self.args) == 1: |
| 71 | return str(self.args[0]) |
| 72 | else: |
| 73 | return str(self.args) |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 74 | |
| 75 | def __getitem__(self, i): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 76 | return self.args[i] |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 77 | |
| 78 | class StandardError(Exception): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 79 | """Base class for all standard Python exceptions.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 80 | pass |
| 81 | |
| 82 | class SyntaxError(StandardError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 83 | """Invalid syntax.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 84 | filename = lineno = offset = text = None |
| 85 | msg = "" |
| 86 | def __init__(self, *args): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +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 |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 96 | def __str__(self): |
| 97 | return str(self.msg) |
| 98 | |
Guido van Rossum | e03c050 | 1998-08-12 02:38:11 +0000 | [diff] [blame] | 99 | class EnvironmentError(StandardError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 100 | """Base class for I/O related errors.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 101 | def __init__(self, *args): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 102 | self.args = args |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 103 | self.errno = None |
| 104 | self.strerror = None |
Guido van Rossum | e03c050 | 1998-08-12 02:38:11 +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 | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 117 | if len(args) == 2: |
| 118 | # common case: PyErr_SetFromErrno() |
Guido van Rossum | e03c050 | 1998-08-12 02:38:11 +0000 | [diff] [blame] | 119 | self.errno, self.strerror = args |
| 120 | |
| 121 | def __str__(self): |
| 122 | if self.filename is not None: |
| 123 | return '[Errno %s] %s: %s' % (self.errno, self.strerror, |
| 124 | repr(self.filename)) |
| 125 | elif self.errno and self.strerror: |
| 126 | return '[Errno %s] %s' % (self.errno, self.strerror) |
| 127 | else: |
| 128 | return StandardError.__str__(self) |
| 129 | |
| 130 | class IOError(EnvironmentError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 131 | """I/O operation failed.""" |
Guido van Rossum | e03c050 | 1998-08-12 02:38:11 +0000 | [diff] [blame] | 132 | pass |
| 133 | |
| 134 | class OSError(EnvironmentError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 135 | """OS system call failed.""" |
Guido van Rossum | e03c050 | 1998-08-12 02:38:11 +0000 | [diff] [blame] | 136 | pass |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 137 | |
| 138 | class RuntimeError(StandardError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 139 | """Unspecified run-time error.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 140 | pass |
| 141 | |
Guido van Rossum | 7ea1d97 | 1998-12-22 13:50:33 +0000 | [diff] [blame] | 142 | class NotImplementedError(RuntimeError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 143 | """Method or function hasn't been implemented yet.""" |
Guido van Rossum | 7ea1d97 | 1998-12-22 13:50:33 +0000 | [diff] [blame] | 144 | pass |
| 145 | |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 146 | class SystemError(StandardError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +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.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 151 | pass |
| 152 | |
| 153 | class EOFError(StandardError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 154 | """Read beyond end of file.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 155 | pass |
| 156 | |
| 157 | class ImportError(StandardError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 158 | """Import can't find module, or can't find name in module.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 159 | pass |
| 160 | |
| 161 | class TypeError(StandardError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 162 | """Inappropriate argument type.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 163 | pass |
| 164 | |
| 165 | class ValueError(StandardError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 166 | """Inappropriate argument value (of correct type).""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 167 | pass |
| 168 | |
| 169 | class KeyboardInterrupt(StandardError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 170 | """Program interrupted by user.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 171 | pass |
| 172 | |
| 173 | class AssertionError(StandardError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 174 | """Assertion failed.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 175 | pass |
| 176 | |
| 177 | class ArithmeticError(StandardError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 178 | """Base class for arithmetic errors.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 179 | pass |
| 180 | |
| 181 | class OverflowError(ArithmeticError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 182 | """Result too large to be represented.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 183 | pass |
| 184 | |
| 185 | class FloatingPointError(ArithmeticError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 186 | """Floating point operation failed.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 187 | pass |
| 188 | |
| 189 | class ZeroDivisionError(ArithmeticError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 190 | """Second argument to a division or modulo operation was zero.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 191 | pass |
| 192 | |
| 193 | class LookupError(StandardError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 194 | """Base class for lookup errors.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 195 | pass |
| 196 | |
| 197 | class IndexError(LookupError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 198 | """Sequence index out of range.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 199 | pass |
| 200 | |
| 201 | class KeyError(LookupError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 202 | """Mapping key not found.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 203 | pass |
| 204 | |
| 205 | class AttributeError(StandardError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 206 | """Attribute not found.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 207 | pass |
| 208 | |
| 209 | class NameError(StandardError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 210 | """Name not found locally or globally.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 211 | pass |
| 212 | |
| 213 | class MemoryError(StandardError): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 214 | """Out of memory.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 215 | pass |
| 216 | |
| 217 | class SystemExit(Exception): |
Guido van Rossum | 3e0d319 | 1999-01-25 21:57:29 +0000 | [diff] [blame^] | 218 | """Request to exit from the interpreter.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 219 | def __init__(self, *args): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 220 | self.args = args |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +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 |