blob: 9bfa0bcee4f1eb91e2ba631e6d3bd074b49483b1 [file] [log] [blame]
Barry Warsaw3e613ce1997-08-29 21:59:26 +00001"""Class based built-in exception hierarchy.
2
Guido van Rossume8fd1431997-09-08 02:47:46 +00003This is a new feature whereby all the standard built-in exceptions,
4traditionally string objects, are replaced with classes. This gives
5Python's exception handling mechanism a more object-oriented feel.
Barry Warsaw3e613ce1997-08-29 21:59:26 +00006
7Most existing code should continue to work with class based
8exceptions. Some tricky uses of IOError may break, but the most
9common uses should work.
10
Guido van Rossume8fd1431997-09-08 02:47:46 +000011To disable this feature, start the Python executable with the -X option.
Barry Warsaw3e613ce1997-08-29 21:59:26 +000012
13Here is a rundown of the class hierarchy. You can change this by
14editing this file, but it isn't recommended. The classes with a `*'
Guido van Rossume8fd1431997-09-08 02:47:46 +000015are new with this feature. They are defined as tuples containing the
16derived exceptions when string-based exceptions are used.
Barry Warsaw3e613ce1997-08-29 21:59:26 +000017
18StandardError(*)
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
47class 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 Rossumf394f561997-09-05 19:00:56 +000060 else:
61 return str(self.args)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000062
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
71class SyntaxError(StandardError):
Guido van Rossumf394f561997-09-05 19:00:56 +000072 filename = lineno = offset = text = None
73 def __init__(self, msg, info=None):
Barry Warsaw3e613ce1997-08-29 21:59:26 +000074 self.msg = msg
Guido van Rossumf394f561997-09-05 19:00:56 +000075 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 Warsaw3e613ce1997-08-29 21:59:26 +000081 def __str__(self):
Guido van Rossumf394f561997-09-05 19:00:56 +000082 return str(self.msg)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000083
84
85class 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
101class RuntimeError(StandardError):
102 pass
103
104class SystemError(StandardError):
105 pass
106
107class EOFError(StandardError):
108 pass
109
110class ImportError(StandardError):
111 pass
112
113class TypeError(StandardError):
114 pass
115
116class ValueError(StandardError):
117 pass
118
119class KeyboardInterrupt(StandardError):
120 pass
121
122class AssertionError(StandardError):
123 pass
124
125class NumberError(StandardError):
126 pass
127
128class OverflowError(NumberError):
129 pass
130
131class FloatingPointError(NumberError):
132 pass
133
134class ZeroDivisionError(NumberError):
135 pass
136
137class LookupError(StandardError):
138 pass
139
140class IndexError(LookupError):
141 pass
142
143class KeyError(LookupError):
144 pass
145
146# debate: should these two inherit from LookupError?
147class AttributeError(StandardError):
148 pass
149
150class NameError(StandardError):
151 pass
152
153class 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
165class MemoryError(StandardError):
166 pass