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