blob: a81ec3cb0ce9fc1eae14318ae671d5cad319730c [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
Guido van Rossumc56ba381997-09-16 18:42:04 +000018Exception(*)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000019 |
Guido van Rossumc56ba381997-09-16 18:42:04 +000020 +-- 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 Warsaw25131fa1997-09-16 21:50:59 +000038 +-- ArithmeticError(*)
Guido van Rossumc56ba381997-09-16 18:42:04 +000039 | |
40 | +-- OverflowError
41 | +-- ZeroDivisionError
42 | +-- FloatingPointError
43 |
44 +-- ValueError
45 +-- SystemError
46 +-- MemoryError
Barry Warsaw3e613ce1997-08-29 21:59:26 +000047"""
48
Guido van Rossumc56ba381997-09-16 18:42:04 +000049class Exception:
Barry Warsaw3e613ce1997-08-29 21:59:26 +000050 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000051 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +000052
53 def __str__(self):
Guido van Rossumc56ba381997-09-16 18:42:04 +000054 if not self.args:
Barry Warsaw3e613ce1997-08-29 21:59:26 +000055 return ''
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000056 elif len(self.args) == 1:
57 return str(self.args[0])
58 else:
59 return str(self.args)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000060
61 def __getitem__(self, i):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000062 return self.args[i]
Barry Warsaw3e613ce1997-08-29 21:59:26 +000063
Fred Drake596db311997-10-06 15:48:20 +000064class StandardError(Exception):
65 pass
66
Barry Warsaw3e613ce1997-08-29 21:59:26 +000067class SyntaxError(StandardError):
Guido van Rossumf394f561997-09-05 19:00:56 +000068 filename = lineno = offset = text = None
Guido van Rossumc56ba381997-09-16 18:42:04 +000069 msg = ""
70 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +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
Barry Warsaw3e613ce1997-08-29 21:59:26 +000080 def __str__(self):
Guido van Rossumf394f561997-09-05 19:00:56 +000081 return str(self.msg)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000082
Barry Warsaw2dfe4de1998-07-23 16:03:46 +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."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +000086 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000087 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +000088 self.errno = None
89 self.strerror = None
Barry Warsaw2dfe4de1998-07-23 16:03:46 +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 Rossumc56ba381997-09-16 18:42:04 +0000102 if len(args) == 2:
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000103 # common case: PyErr_SetFromErrno()
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000104 self.errno, self.strerror = args
105
106 def __str__(self):
107 if self.filename:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000108 return '[Errno %s] %s: %s' % (self.errno, self.strerror,
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000109 self.filename)
110 elif self.errno and self.strerror:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000111 return '[Errno %s] %s' % (self.errno, self.strerror)
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000112 else:
113 return StandardError.__str__(self)
114
115class IOError(EnvironmentError):
116 pass
117
118class OSError(EnvironmentError):
119 """Used by the posix module."""
120 pass
Barry Warsaw3e613ce1997-08-29 21:59:26 +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
Barry Warsaw25131fa1997-09-16 21:50:59 +0000146class ArithmeticError(StandardError):
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000147 pass
148
Barry Warsaw25131fa1997-09-16 21:50:59 +0000149class OverflowError(ArithmeticError):
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000150 pass
151
Barry Warsaw25131fa1997-09-16 21:50:59 +0000152class FloatingPointError(ArithmeticError):
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000153 pass
154
Barry Warsaw25131fa1997-09-16 21:50:59 +0000155class ZeroDivisionError(ArithmeticError):
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000156 pass
157
158class LookupError(StandardError):
159 pass
160
161class IndexError(LookupError):
162 pass
163
164class KeyError(LookupError):
165 pass
166
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000167class AttributeError(StandardError):
168 pass
169
170class NameError(StandardError):
171 pass
172
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000173class MemoryError(StandardError):
174 pass
Guido van Rossumc56ba381997-09-16 18:42:04 +0000175
176class SystemExit(Exception):
177 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000178 self.args = args
Guido van Rossumc56ba381997-09-16 18:42:04 +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