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: |
| 63 | def __init__(self, *args): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 64 | self.args = args |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 65 | |
| 66 | def __str__(self): |
| 67 | if not self.args: |
| 68 | return '' |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 69 | elif len(self.args) == 1: |
| 70 | return str(self.args[0]) |
| 71 | else: |
| 72 | return str(self.args) |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 73 | |
| 74 | def __getitem__(self, i): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 75 | return self.args[i] |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 76 | |
| 77 | class StandardError(Exception): |
| 78 | pass |
| 79 | |
| 80 | class SyntaxError(StandardError): |
| 81 | filename = lineno = offset = text = None |
| 82 | msg = "" |
| 83 | def __init__(self, *args): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 84 | self.args = args |
| 85 | if len(self.args) >= 1: |
| 86 | self.msg = self.args[0] |
| 87 | if len(self.args) == 2: |
| 88 | info = self.args[1] |
| 89 | try: |
| 90 | self.filename, self.lineno, self.offset, self.text = info |
| 91 | except: |
| 92 | pass |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 93 | def __str__(self): |
| 94 | return str(self.msg) |
| 95 | |
Guido van Rossum | e03c050 | 1998-08-12 02:38:11 +0000 | [diff] [blame] | 96 | class EnvironmentError(StandardError): |
| 97 | """Base class for exceptions that occur outside the Python system. |
| 98 | Primarily used as a base class for OSError and IOError.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 99 | def __init__(self, *args): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 100 | self.args = args |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 101 | self.errno = None |
| 102 | self.strerror = None |
Guido van Rossum | e03c050 | 1998-08-12 02:38:11 +0000 | [diff] [blame] | 103 | self.filename = None |
| 104 | if len(args) == 3: |
| 105 | # open() errors give third argument which is the filename. BUT, |
| 106 | # so common in-place unpacking doesn't break, e.g.: |
| 107 | # |
| 108 | # except IOError, (errno, strerror): |
| 109 | # |
| 110 | # we hack args so that it only contains two items. This also |
| 111 | # means we need our own __str__() which prints out the filename |
| 112 | # when it was supplied. |
| 113 | self.errno, self.strerror, self.filename = args |
| 114 | self.args = args[0:2] |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 115 | if len(args) == 2: |
| 116 | # common case: PyErr_SetFromErrno() |
Guido van Rossum | e03c050 | 1998-08-12 02:38:11 +0000 | [diff] [blame] | 117 | self.errno, self.strerror = args |
| 118 | |
| 119 | def __str__(self): |
| 120 | if self.filename is not None: |
| 121 | return '[Errno %s] %s: %s' % (self.errno, self.strerror, |
| 122 | repr(self.filename)) |
| 123 | elif self.errno and self.strerror: |
| 124 | return '[Errno %s] %s' % (self.errno, self.strerror) |
| 125 | else: |
| 126 | return StandardError.__str__(self) |
| 127 | |
| 128 | class IOError(EnvironmentError): |
| 129 | pass |
| 130 | |
| 131 | class OSError(EnvironmentError): |
| 132 | """Used by the posix module.""" |
| 133 | pass |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 134 | |
| 135 | class RuntimeError(StandardError): |
| 136 | pass |
| 137 | |
Guido van Rossum | 7ea1d97 | 1998-12-22 13:50:33 +0000 | [diff] [blame] | 138 | class NotImplementedError(RuntimeError): |
| 139 | pass |
| 140 | |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 141 | class SystemError(StandardError): |
| 142 | pass |
| 143 | |
| 144 | class EOFError(StandardError): |
| 145 | pass |
| 146 | |
| 147 | class ImportError(StandardError): |
| 148 | pass |
| 149 | |
| 150 | class TypeError(StandardError): |
| 151 | pass |
| 152 | |
| 153 | class ValueError(StandardError): |
| 154 | pass |
| 155 | |
| 156 | class KeyboardInterrupt(StandardError): |
| 157 | pass |
| 158 | |
| 159 | class AssertionError(StandardError): |
| 160 | pass |
| 161 | |
| 162 | class ArithmeticError(StandardError): |
| 163 | pass |
| 164 | |
| 165 | class OverflowError(ArithmeticError): |
| 166 | pass |
| 167 | |
| 168 | class FloatingPointError(ArithmeticError): |
| 169 | pass |
| 170 | |
| 171 | class ZeroDivisionError(ArithmeticError): |
| 172 | pass |
| 173 | |
| 174 | class LookupError(StandardError): |
| 175 | pass |
| 176 | |
| 177 | class IndexError(LookupError): |
| 178 | pass |
| 179 | |
| 180 | class KeyError(LookupError): |
| 181 | pass |
| 182 | |
| 183 | class AttributeError(StandardError): |
| 184 | pass |
| 185 | |
| 186 | class NameError(StandardError): |
| 187 | pass |
| 188 | |
| 189 | class MemoryError(StandardError): |
| 190 | pass |
| 191 | |
| 192 | class SystemExit(Exception): |
| 193 | def __init__(self, *args): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 194 | self.args = args |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 195 | if len(args) == 0: |
| 196 | self.code = None |
| 197 | elif len(args) == 1: |
| 198 | self.code = args[0] |
| 199 | else: |
| 200 | self.code = args |