Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 1 | """Class based built-in exception hierarchy. |
| 2 | |
Guido van Rossum | e8fd143 | 1997-09-08 02:47:46 +0000 | [diff] [blame^] | 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. |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | e8fd143 | 1997-09-08 02:47:46 +0000 | [diff] [blame^] | 11 | To disable this feature, start the Python executable with the -X option. |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 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 `*' |
Guido van Rossum | e8fd143 | 1997-09-08 02:47:46 +0000 | [diff] [blame^] | 15 | are new with this feature. They are defined as tuples containing the |
| 16 | derived exceptions when string-based exceptions are used. |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 17 | |
| 18 | StandardError(*) |
| 19 | | |
| 20 | +-- SystemExit |
| 21 | +-- KeyboardInterrupt |
| 22 | +-- ImportError |
| 23 | +-- IOError |
| 24 | +-- EOFError |
| 25 | +-- RuntimeError |
| 26 | +-- NameError |
| 27 | +-- AttributeError |
| 28 | +-- SyntaxError |
| 29 | +-- TypeError |
| 30 | +-- AssertionError |
| 31 | +-- LookupError(*) |
| 32 | | | |
| 33 | | +-- IndexError |
| 34 | | +-- KeyError |
| 35 | | |
| 36 | +-- NumberError(*) |
| 37 | | | |
| 38 | | +-- OverflowError |
| 39 | | +-- ZeroDivisionError |
| 40 | | +-- FloatingPointError |
| 41 | | |
| 42 | +-- ValueError |
| 43 | +-- SystemError |
| 44 | +-- MemoryError |
| 45 | """ |
| 46 | |
| 47 | class StandardError: |
| 48 | def __init__(self, *args): |
| 49 | if len(args) == 0: |
| 50 | self.args = None |
| 51 | elif len(args) == 1: |
| 52 | # de-tuplify |
| 53 | self.args = args[0] |
| 54 | else: |
| 55 | self.args = args |
| 56 | |
| 57 | def __str__(self): |
| 58 | if self.args == None: |
| 59 | return '' |
Guido van Rossum | f394f56 | 1997-09-05 19:00:56 +0000 | [diff] [blame] | 60 | else: |
| 61 | return str(self.args) |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 62 | |
| 63 | def __getitem__(self, i): |
| 64 | if type(self.args) == type(()): |
| 65 | return self.args[i] |
| 66 | elif i == 0: |
| 67 | return self.args |
| 68 | else: |
| 69 | raise IndexError |
| 70 | |
| 71 | class SyntaxError(StandardError): |
Guido van Rossum | f394f56 | 1997-09-05 19:00:56 +0000 | [diff] [blame] | 72 | filename = lineno = offset = text = None |
| 73 | def __init__(self, msg, info=None): |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 74 | self.msg = msg |
Guido van Rossum | f394f56 | 1997-09-05 19:00:56 +0000 | [diff] [blame] | 75 | if info: |
| 76 | self.args = msg |
| 77 | else: |
| 78 | self.args = (msg, info) |
| 79 | if info: |
| 80 | self.filename, self.lineno, self.offset, self.text = info |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 81 | def __str__(self): |
Guido van Rossum | f394f56 | 1997-09-05 19:00:56 +0000 | [diff] [blame] | 82 | return str(self.msg) |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | class IOError(StandardError): |
| 86 | def __init__(self, *args): |
| 87 | self.errno = None |
| 88 | self.strerror = None |
| 89 | if len(args) == 1: |
| 90 | # de-tuplify |
| 91 | self.args = args[0] |
| 92 | elif len(args) == 2: |
| 93 | # common case: PyErr_SetFromErrno() |
| 94 | self.args = args |
| 95 | self.errno = args[0] |
| 96 | self.strerror = args[1] |
| 97 | else: |
| 98 | self.args = args |
| 99 | |
| 100 | |
| 101 | class RuntimeError(StandardError): |
| 102 | pass |
| 103 | |
| 104 | class SystemError(StandardError): |
| 105 | pass |
| 106 | |
| 107 | class EOFError(StandardError): |
| 108 | pass |
| 109 | |
| 110 | class ImportError(StandardError): |
| 111 | pass |
| 112 | |
| 113 | class TypeError(StandardError): |
| 114 | pass |
| 115 | |
| 116 | class ValueError(StandardError): |
| 117 | pass |
| 118 | |
| 119 | class KeyboardInterrupt(StandardError): |
| 120 | pass |
| 121 | |
| 122 | class AssertionError(StandardError): |
| 123 | pass |
| 124 | |
| 125 | class NumberError(StandardError): |
| 126 | pass |
| 127 | |
| 128 | class OverflowError(NumberError): |
| 129 | pass |
| 130 | |
| 131 | class FloatingPointError(NumberError): |
| 132 | pass |
| 133 | |
| 134 | class ZeroDivisionError(NumberError): |
| 135 | pass |
| 136 | |
| 137 | class LookupError(StandardError): |
| 138 | pass |
| 139 | |
| 140 | class IndexError(LookupError): |
| 141 | pass |
| 142 | |
| 143 | class KeyError(LookupError): |
| 144 | pass |
| 145 | |
| 146 | # debate: should these two inherit from LookupError? |
| 147 | class AttributeError(StandardError): |
| 148 | pass |
| 149 | |
| 150 | class NameError(StandardError): |
| 151 | pass |
| 152 | |
| 153 | class SystemExit(StandardError): |
| 154 | def __init__(self, *args): |
| 155 | if len(args) == 0: |
| 156 | self.args = None |
| 157 | elif len(args) == 1: |
| 158 | # de-tuplify |
| 159 | self.args = args[0] |
| 160 | else: |
| 161 | self.args = args |
| 162 | self.code = self.args |
| 163 | |
| 164 | |
| 165 | class MemoryError(StandardError): |
| 166 | pass |