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 |
| 15 | to be found by the bltinmodule.c file. |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 64e736b | 1998-10-02 01:23:47 +0000 | [diff] [blame] | 17 | The classes with a `*' are new as of Python 1.5. They are defined as tuples |
| 18 | containing the derived exceptions when string-based exceptions are used. If |
| 19 | you define your own class based exceptions, they should be derived from |
| 20 | Exception. |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 21 | |
| 22 | Exception(*) |
| 23 | | |
| 24 | +-- StandardError(*) |
| 25 | | |
| 26 | +-- SystemExit |
| 27 | +-- KeyboardInterrupt |
| 28 | +-- ImportError |
Guido van Rossum | 64e736b | 1998-10-02 01:23:47 +0000 | [diff] [blame] | 29 | +-- EnvironmentError(*) |
| 30 | | | |
| 31 | | +-- IOError |
| 32 | | +-- OSError(*) |
| 33 | | |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 34 | +-- EOFError |
| 35 | +-- RuntimeError |
| 36 | +-- NameError |
| 37 | +-- AttributeError |
| 38 | +-- SyntaxError |
| 39 | +-- TypeError |
| 40 | +-- AssertionError |
| 41 | +-- LookupError(*) |
| 42 | | | |
| 43 | | +-- IndexError |
| 44 | | +-- KeyError |
| 45 | | |
| 46 | +-- ArithmeticError(*) |
| 47 | | | |
| 48 | | +-- OverflowError |
| 49 | | +-- ZeroDivisionError |
| 50 | | +-- FloatingPointError |
| 51 | | |
| 52 | +-- ValueError |
| 53 | +-- SystemError |
| 54 | +-- MemoryError |
| 55 | """ |
| 56 | |
| 57 | class Exception: |
| 58 | def __init__(self, *args): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 59 | self.args = args |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 60 | |
| 61 | def __str__(self): |
| 62 | if not self.args: |
| 63 | return '' |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 64 | elif len(self.args) == 1: |
| 65 | return str(self.args[0]) |
| 66 | else: |
| 67 | return str(self.args) |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 68 | |
| 69 | def __getitem__(self, i): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 70 | return self.args[i] |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 71 | |
| 72 | class StandardError(Exception): |
| 73 | pass |
| 74 | |
| 75 | class SyntaxError(StandardError): |
| 76 | filename = lineno = offset = text = None |
| 77 | msg = "" |
| 78 | def __init__(self, *args): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 79 | self.args = args |
| 80 | if len(self.args) >= 1: |
| 81 | self.msg = self.args[0] |
| 82 | if len(self.args) == 2: |
| 83 | info = self.args[1] |
| 84 | try: |
| 85 | self.filename, self.lineno, self.offset, self.text = info |
| 86 | except: |
| 87 | pass |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 88 | def __str__(self): |
| 89 | return str(self.msg) |
| 90 | |
Guido van Rossum | e03c050 | 1998-08-12 02:38:11 +0000 | [diff] [blame] | 91 | class EnvironmentError(StandardError): |
| 92 | """Base class for exceptions that occur outside the Python system. |
| 93 | Primarily used as a base class for OSError and IOError.""" |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 94 | def __init__(self, *args): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 95 | self.args = args |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 96 | self.errno = None |
| 97 | self.strerror = None |
Guido van Rossum | e03c050 | 1998-08-12 02:38:11 +0000 | [diff] [blame] | 98 | self.filename = None |
| 99 | if len(args) == 3: |
| 100 | # open() errors give third argument which is the filename. BUT, |
| 101 | # so common in-place unpacking doesn't break, e.g.: |
| 102 | # |
| 103 | # except IOError, (errno, strerror): |
| 104 | # |
| 105 | # we hack args so that it only contains two items. This also |
| 106 | # means we need our own __str__() which prints out the filename |
| 107 | # when it was supplied. |
| 108 | self.errno, self.strerror, self.filename = args |
| 109 | self.args = args[0:2] |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 110 | if len(args) == 2: |
| 111 | # common case: PyErr_SetFromErrno() |
Guido van Rossum | e03c050 | 1998-08-12 02:38:11 +0000 | [diff] [blame] | 112 | self.errno, self.strerror = args |
| 113 | |
| 114 | def __str__(self): |
| 115 | if self.filename is not None: |
| 116 | return '[Errno %s] %s: %s' % (self.errno, self.strerror, |
| 117 | repr(self.filename)) |
| 118 | elif self.errno and self.strerror: |
| 119 | return '[Errno %s] %s' % (self.errno, self.strerror) |
| 120 | else: |
| 121 | return StandardError.__str__(self) |
| 122 | |
| 123 | class IOError(EnvironmentError): |
| 124 | pass |
| 125 | |
| 126 | class OSError(EnvironmentError): |
| 127 | """Used by the posix module.""" |
| 128 | pass |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 129 | |
| 130 | class RuntimeError(StandardError): |
| 131 | pass |
| 132 | |
| 133 | class SystemError(StandardError): |
| 134 | pass |
| 135 | |
| 136 | class EOFError(StandardError): |
| 137 | pass |
| 138 | |
| 139 | class ImportError(StandardError): |
| 140 | pass |
| 141 | |
| 142 | class TypeError(StandardError): |
| 143 | pass |
| 144 | |
| 145 | class ValueError(StandardError): |
| 146 | pass |
| 147 | |
| 148 | class KeyboardInterrupt(StandardError): |
| 149 | pass |
| 150 | |
| 151 | class AssertionError(StandardError): |
| 152 | pass |
| 153 | |
| 154 | class ArithmeticError(StandardError): |
| 155 | pass |
| 156 | |
| 157 | class OverflowError(ArithmeticError): |
| 158 | pass |
| 159 | |
| 160 | class FloatingPointError(ArithmeticError): |
| 161 | pass |
| 162 | |
| 163 | class ZeroDivisionError(ArithmeticError): |
| 164 | pass |
| 165 | |
| 166 | class LookupError(StandardError): |
| 167 | pass |
| 168 | |
| 169 | class IndexError(LookupError): |
| 170 | pass |
| 171 | |
| 172 | class KeyError(LookupError): |
| 173 | pass |
| 174 | |
| 175 | class AttributeError(StandardError): |
| 176 | pass |
| 177 | |
| 178 | class NameError(StandardError): |
| 179 | pass |
| 180 | |
| 181 | class MemoryError(StandardError): |
| 182 | pass |
| 183 | |
| 184 | class SystemExit(Exception): |
| 185 | def __init__(self, *args): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 186 | self.args = args |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 187 | if len(args) == 0: |
| 188 | self.code = None |
| 189 | elif len(args) == 1: |
| 190 | self.code = args[0] |
| 191 | else: |
| 192 | self.code = args |