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 | |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 18 | Exception(*) |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 19 | | |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 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 | | |
Barry Warsaw | 25131fa | 1997-09-16 21:50:59 +0000 | [diff] [blame] | 38 | +-- ArithmeticError(*) |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 39 | | | |
| 40 | | +-- OverflowError |
| 41 | | +-- ZeroDivisionError |
| 42 | | +-- FloatingPointError |
| 43 | | |
| 44 | +-- ValueError |
| 45 | +-- SystemError |
| 46 | +-- MemoryError |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 47 | """ |
| 48 | |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 49 | class Exception: |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 50 | def __init__(self, *args): |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 51 | self.args = args |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 52 | |
| 53 | def __str__(self): |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 54 | if not self.args: |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 55 | return '' |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 56 | elif len(self.args) == 1: |
| 57 | return str(self.args[0]) |
Guido van Rossum | f394f56 | 1997-09-05 19:00:56 +0000 | [diff] [blame] | 58 | else: |
| 59 | return str(self.args) |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 61 | class StandardError(Exception): |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 62 | def __getitem__(self, i): |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 63 | return self.args[i] |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 64 | |
| 65 | class SyntaxError(StandardError): |
Guido van Rossum | f394f56 | 1997-09-05 19:00:56 +0000 | [diff] [blame] | 66 | filename = lineno = offset = text = None |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 67 | msg = "" |
| 68 | def __init__(self, *args): |
| 69 | self.args = args |
| 70 | if len(self.args) >= 1: |
| 71 | self.msg = self.args[0] |
| 72 | if len(self.args) == 2: |
| 73 | info = self.args[1] |
| 74 | try: |
| 75 | self.filename, self.lineno, self.offset, self.text = info |
| 76 | except: |
| 77 | pass |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 78 | def __str__(self): |
Guido van Rossum | f394f56 | 1997-09-05 19:00:56 +0000 | [diff] [blame] | 79 | return str(self.msg) |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 80 | |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 81 | class IOError(StandardError): |
| 82 | def __init__(self, *args): |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 83 | self.args = args |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 84 | self.errno = None |
| 85 | self.strerror = None |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 86 | if len(args) == 2: |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 87 | # common case: PyErr_SetFromErrno() |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 88 | self.errno = args[0] |
| 89 | self.strerror = args[1] |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 90 | |
| 91 | class RuntimeError(StandardError): |
| 92 | pass |
| 93 | |
| 94 | class SystemError(StandardError): |
| 95 | pass |
| 96 | |
| 97 | class EOFError(StandardError): |
| 98 | pass |
| 99 | |
| 100 | class ImportError(StandardError): |
| 101 | pass |
| 102 | |
| 103 | class TypeError(StandardError): |
| 104 | pass |
| 105 | |
| 106 | class ValueError(StandardError): |
| 107 | pass |
| 108 | |
| 109 | class KeyboardInterrupt(StandardError): |
| 110 | pass |
| 111 | |
| 112 | class AssertionError(StandardError): |
| 113 | pass |
| 114 | |
Barry Warsaw | 25131fa | 1997-09-16 21:50:59 +0000 | [diff] [blame] | 115 | class ArithmeticError(StandardError): |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 116 | pass |
| 117 | |
Barry Warsaw | 25131fa | 1997-09-16 21:50:59 +0000 | [diff] [blame] | 118 | class OverflowError(ArithmeticError): |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 119 | pass |
| 120 | |
Barry Warsaw | 25131fa | 1997-09-16 21:50:59 +0000 | [diff] [blame] | 121 | class FloatingPointError(ArithmeticError): |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 122 | pass |
| 123 | |
Barry Warsaw | 25131fa | 1997-09-16 21:50:59 +0000 | [diff] [blame] | 124 | class ZeroDivisionError(ArithmeticError): |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 125 | pass |
| 126 | |
| 127 | class LookupError(StandardError): |
| 128 | pass |
| 129 | |
| 130 | class IndexError(LookupError): |
| 131 | pass |
| 132 | |
| 133 | class KeyError(LookupError): |
| 134 | pass |
| 135 | |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 136 | class AttributeError(StandardError): |
| 137 | pass |
| 138 | |
| 139 | class NameError(StandardError): |
| 140 | pass |
| 141 | |
Barry Warsaw | 3e613ce | 1997-08-29 21:59:26 +0000 | [diff] [blame] | 142 | class MemoryError(StandardError): |
| 143 | pass |
Guido van Rossum | c56ba38 | 1997-09-16 18:42:04 +0000 | [diff] [blame] | 144 | |
| 145 | class SystemExit(Exception): |
| 146 | def __init__(self, *args): |
| 147 | self.args = args |
| 148 | if len(args) == 0: |
| 149 | self.code = None |
| 150 | elif len(args) == 1: |
| 151 | self.code = args[0] |
| 152 | else: |
| 153 | self.code = args |