blob: 28711df63006628b5ea30d849f075cc5534254c3 [file] [log] [blame]
Guido van Rossuma11cccc1997-10-06 20:19:59 +00001"""Class based built-in exception hierarchy.
2
3This 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.
6
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
11To disable this feature, start the Python executable with the -X option.
12
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 `*'
15are new with this feature. They are defined as tuples containing the
16derived exceptions when string-based exceptions are used.
17
18Exception(*)
19 |
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 |
38 +-- ArithmeticError(*)
39 | |
40 | +-- OverflowError
41 | +-- ZeroDivisionError
42 | +-- FloatingPointError
43 |
44 +-- ValueError
45 +-- SystemError
46 +-- MemoryError
47"""
48
49class Exception:
50 def __init__(self, *args):
Guido van Rossum548703a1998-03-26 22:14:20 +000051 self.args = args
Guido van Rossuma11cccc1997-10-06 20:19:59 +000052
53 def __str__(self):
54 if not self.args:
55 return ''
Guido van Rossum548703a1998-03-26 22:14:20 +000056 elif len(self.args) == 1:
57 return str(self.args[0])
58 else:
59 return str(self.args)
Guido van Rossuma11cccc1997-10-06 20:19:59 +000060
61 def __getitem__(self, i):
Guido van Rossum548703a1998-03-26 22:14:20 +000062 return self.args[i]
Guido van Rossuma11cccc1997-10-06 20:19:59 +000063
64class StandardError(Exception):
65 pass
66
67class SyntaxError(StandardError):
68 filename = lineno = offset = text = None
69 msg = ""
70 def __init__(self, *args):
Guido van Rossum548703a1998-03-26 22:14:20 +000071 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
Guido van Rossuma11cccc1997-10-06 20:19:59 +000080 def __str__(self):
81 return str(self.msg)
82
Guido van Rossume03c0501998-08-12 02:38:11 +000083class EnvironmentError(StandardError):
84 """Base class for exceptions that occur outside the Python system.
85 Primarily used as a base class for OSError and IOError."""
Guido van Rossuma11cccc1997-10-06 20:19:59 +000086 def __init__(self, *args):
Guido van Rossum548703a1998-03-26 22:14:20 +000087 self.args = args
Guido van Rossuma11cccc1997-10-06 20:19:59 +000088 self.errno = None
89 self.strerror = None
Guido van Rossume03c0501998-08-12 02:38:11 +000090 self.filename = None
91 if len(args) == 3:
92 # open() errors give third argument which is the filename. BUT,
93 # so common in-place unpacking doesn't break, e.g.:
94 #
95 # except IOError, (errno, strerror):
96 #
97 # we hack args so that it only contains two items. This also
98 # means we need our own __str__() which prints out the filename
99 # when it was supplied.
100 self.errno, self.strerror, self.filename = args
101 self.args = args[0:2]
Guido van Rossuma11cccc1997-10-06 20:19:59 +0000102 if len(args) == 2:
103 # common case: PyErr_SetFromErrno()
Guido van Rossume03c0501998-08-12 02:38:11 +0000104 self.errno, self.strerror = args
105
106 def __str__(self):
107 if self.filename is not None:
108 return '[Errno %s] %s: %s' % (self.errno, self.strerror,
109 repr(self.filename))
110 elif self.errno and self.strerror:
111 return '[Errno %s] %s' % (self.errno, self.strerror)
112 else:
113 return StandardError.__str__(self)
114
115class IOError(EnvironmentError):
116 pass
117
118class OSError(EnvironmentError):
119 """Used by the posix module."""
120 pass
Guido van Rossuma11cccc1997-10-06 20:19:59 +0000121
122class RuntimeError(StandardError):
123 pass
124
125class SystemError(StandardError):
126 pass
127
128class EOFError(StandardError):
129 pass
130
131class ImportError(StandardError):
132 pass
133
134class TypeError(StandardError):
135 pass
136
137class ValueError(StandardError):
138 pass
139
140class KeyboardInterrupt(StandardError):
141 pass
142
143class AssertionError(StandardError):
144 pass
145
146class ArithmeticError(StandardError):
147 pass
148
149class OverflowError(ArithmeticError):
150 pass
151
152class FloatingPointError(ArithmeticError):
153 pass
154
155class ZeroDivisionError(ArithmeticError):
156 pass
157
158class LookupError(StandardError):
159 pass
160
161class IndexError(LookupError):
162 pass
163
164class KeyError(LookupError):
165 pass
166
167class AttributeError(StandardError):
168 pass
169
170class NameError(StandardError):
171 pass
172
173class MemoryError(StandardError):
174 pass
175
176class SystemExit(Exception):
177 def __init__(self, *args):
Guido van Rossum548703a1998-03-26 22:14:20 +0000178 self.args = args
Guido van Rossuma11cccc1997-10-06 20:19:59 +0000179 if len(args) == 0:
180 self.code = None
181 elif len(args) == 1:
182 self.code = args[0]
183 else:
184 self.code = args