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