blob: 37b9590467ec36bbbabb4ef2155c24855526fab5 [file] [log] [blame]
Barry Warsaw3e613ce1997-08-29 21:59:26 +00001"""Class based built-in exception hierarchy.
2
3This is an experimental new feature whereby all the standard built-in
4exceptions, traditionally string object, are replaced with classes.
5This gives Python's exception handling mechanism a more
6object-oriented feel.
7
8Most existing code should continue to work with class based
9exceptions. Some tricky uses of IOError may break, but the most
10common uses should work.
11
12To use this new feature, start the python executable with the -X option.
13
14Here is a rundown of the class hierarchy. You can change this by
15editing this file, but it isn't recommended. The classes with a `*'
16are new with this feature and are not available unless class based
17exceptions are used.
18
19StandardError(*)
20 |
21 +-- SystemExit
22 +-- KeyboardInterrupt
23 +-- ImportError
24 +-- IOError
25 +-- EOFError
26 +-- RuntimeError
27 +-- NameError
28 +-- AttributeError
29 +-- SyntaxError
30 +-- TypeError
31 +-- AssertionError
32 +-- LookupError(*)
33 | |
34 | +-- IndexError
35 | +-- KeyError
36 |
37 +-- NumberError(*)
38 | |
39 | +-- OverflowError
40 | +-- ZeroDivisionError
41 | +-- FloatingPointError
42 |
43 +-- ValueError
44 +-- SystemError
45 +-- MemoryError
46"""
47
48class StandardError:
49 def __init__(self, *args):
50 if len(args) == 0:
51 self.args = None
52 elif len(args) == 1:
53 # de-tuplify
54 self.args = args[0]
55 else:
56 self.args = args
57
58 def __str__(self):
59 if self.args == None:
60 return ''
Guido van Rossumf394f561997-09-05 19:00:56 +000061 else:
62 return str(self.args)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000063
64 def __getitem__(self, i):
65 if type(self.args) == type(()):
66 return self.args[i]
67 elif i == 0:
68 return self.args
69 else:
70 raise IndexError
71
72class SyntaxError(StandardError):
Guido van Rossumf394f561997-09-05 19:00:56 +000073 filename = lineno = offset = text = None
74 def __init__(self, msg, info=None):
Barry Warsaw3e613ce1997-08-29 21:59:26 +000075 self.msg = msg
Guido van Rossumf394f561997-09-05 19:00:56 +000076 if info:
77 self.args = msg
78 else:
79 self.args = (msg, info)
80 if info:
81 self.filename, self.lineno, self.offset, self.text = info
Barry Warsaw3e613ce1997-08-29 21:59:26 +000082 def __str__(self):
Guido van Rossumf394f561997-09-05 19:00:56 +000083 return str(self.msg)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000084
85
86class IOError(StandardError):
87 def __init__(self, *args):
88 self.errno = None
89 self.strerror = None
90 if len(args) == 1:
91 # de-tuplify
92 self.args = args[0]
93 elif len(args) == 2:
94 # common case: PyErr_SetFromErrno()
95 self.args = args
96 self.errno = args[0]
97 self.strerror = args[1]
98 else:
99 self.args = args
100
101
102class RuntimeError(StandardError):
103 pass
104
105class SystemError(StandardError):
106 pass
107
108class EOFError(StandardError):
109 pass
110
111class ImportError(StandardError):
112 pass
113
114class TypeError(StandardError):
115 pass
116
117class ValueError(StandardError):
118 pass
119
120class KeyboardInterrupt(StandardError):
121 pass
122
123class AssertionError(StandardError):
124 pass
125
126class NumberError(StandardError):
127 pass
128
129class OverflowError(NumberError):
130 pass
131
132class FloatingPointError(NumberError):
133 pass
134
135class ZeroDivisionError(NumberError):
136 pass
137
138class LookupError(StandardError):
139 pass
140
141class IndexError(LookupError):
142 pass
143
144class KeyError(LookupError):
145 pass
146
147# debate: should these two inherit from LookupError?
148class AttributeError(StandardError):
149 pass
150
151class NameError(StandardError):
152 pass
153
154class SystemExit(StandardError):
155 def __init__(self, *args):
156 if len(args) == 0:
157 self.args = None
158 elif len(args) == 1:
159 # de-tuplify
160 self.args = args[0]
161 else:
162 self.args = args
163 self.code = self.args
164
165
166class MemoryError(StandardError):
167 pass