blob: 4b039692a7ca5461d984d63a2e6c110ba0038dbf [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 ''
61 elif type(self.args) == type(''):
62 return self.args
63 else:
64 return `self.args`
65
66 def __getitem__(self, i):
67 if type(self.args) == type(()):
68 return self.args[i]
69 elif i == 0:
70 return self.args
71 else:
72 raise IndexError
73
74class SyntaxError(StandardError):
75 def __init__(self, msg, info):
76 self.msg = msg
77 self.filename, self.lineno, self.offset, self.text = info
78
79 def __str__(self):
80 return msg
81
82
83class IOError(StandardError):
84 def __init__(self, *args):
85 self.errno = None
86 self.strerror = None
87 if len(args) == 1:
88 # de-tuplify
89 self.args = args[0]
90 elif len(args) == 2:
91 # common case: PyErr_SetFromErrno()
92 self.args = args
93 self.errno = args[0]
94 self.strerror = args[1]
95 else:
96 self.args = args
97
98
99class RuntimeError(StandardError):
100 pass
101
102class SystemError(StandardError):
103 pass
104
105class EOFError(StandardError):
106 pass
107
108class ImportError(StandardError):
109 pass
110
111class TypeError(StandardError):
112 pass
113
114class ValueError(StandardError):
115 pass
116
117class KeyboardInterrupt(StandardError):
118 pass
119
120class AssertionError(StandardError):
121 pass
122
123class NumberError(StandardError):
124 pass
125
126class OverflowError(NumberError):
127 pass
128
129class FloatingPointError(NumberError):
130 pass
131
132class ZeroDivisionError(NumberError):
133 pass
134
135class LookupError(StandardError):
136 pass
137
138class IndexError(LookupError):
139 pass
140
141class KeyError(LookupError):
142 pass
143
144# debate: should these two inherit from LookupError?
145class AttributeError(StandardError):
146 pass
147
148class NameError(StandardError):
149 pass
150
151class SystemExit(StandardError):
152 def __init__(self, *args):
153 if len(args) == 0:
154 self.args = None
155 elif len(args) == 1:
156 # de-tuplify
157 self.args = args[0]
158 else:
159 self.args = args
160 self.code = self.args
161
162
163class MemoryError(StandardError):
164 pass