blob: 9c733ce2e96ccfb4138187866e3a4042f285ae65 [file] [log] [blame]
Barry Warsaw3e613ce1997-08-29 21:59:26 +00001"""Class based built-in exception hierarchy.
2
Barry Warsaw9195f551998-09-25 22:43:21 +00003New with Python 1.5, all standard built-in exceptions are now class objects by
4default. This gives Python's exception handling mechanism a more
5object-oriented feel. Traditionally they were string objects. Python will
6fallback to string based exceptions if the interpreter is invoked with the -X
7option, or if some failure occurs during class exception initialization (in
8this case a warning will be printed).
Barry Warsaw3e613ce1997-08-29 21:59:26 +00009
Barry Warsaw9195f551998-09-25 22:43:21 +000010Most existing code should continue to work with class based exceptions. Some
11tricky uses of IOError may break, but the most common uses should work.
Barry Warsaw3e613ce1997-08-29 21:59:26 +000012
Barry Warsaw9195f551998-09-25 22:43:21 +000013Here is a rundown of the class hierarchy. You can change this by editing this
14file, but it isn't recommended. The class names described here are expected
15to be found by the bltinmodule.c file.
Barry Warsaw3e613ce1997-08-29 21:59:26 +000016
Barry Warsaw9195f551998-09-25 22:43:21 +000017The classes with a `*' are new as of Python 1.5. They are defined as tuples
18containing the derived exceptions when string-based exceptions are used. If
19you define your own class based exceptions, they should be derived from
20Exception.
Barry Warsaw3e613ce1997-08-29 21:59:26 +000021
Guido van Rossumc56ba381997-09-16 18:42:04 +000022Exception(*)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000023 |
Guido van Rossumc56ba381997-09-16 18:42:04 +000024 +-- StandardError(*)
25 |
26 +-- SystemExit
27 +-- KeyboardInterrupt
28 +-- ImportError
Barry Warsaw9195f551998-09-25 22:43:21 +000029 +-- EnvironmentError(*)
30 | |
31 | +-- IOError
32 | +-- OSError(*)
33 |
Guido van Rossumc56ba381997-09-16 18:42:04 +000034 +-- EOFError
35 +-- RuntimeError
36 +-- NameError
37 +-- AttributeError
38 +-- SyntaxError
39 +-- TypeError
40 +-- AssertionError
41 +-- LookupError(*)
42 | |
43 | +-- IndexError
44 | +-- KeyError
45 |
Barry Warsaw25131fa1997-09-16 21:50:59 +000046 +-- ArithmeticError(*)
Guido van Rossumc56ba381997-09-16 18:42:04 +000047 | |
48 | +-- OverflowError
49 | +-- ZeroDivisionError
50 | +-- FloatingPointError
51 |
52 +-- ValueError
53 +-- SystemError
54 +-- MemoryError
Barry Warsaw3e613ce1997-08-29 21:59:26 +000055"""
56
Guido van Rossumc56ba381997-09-16 18:42:04 +000057class Exception:
Barry Warsaw3e613ce1997-08-29 21:59:26 +000058 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000059 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +000060
61 def __str__(self):
Guido van Rossumc56ba381997-09-16 18:42:04 +000062 if not self.args:
Barry Warsaw3e613ce1997-08-29 21:59:26 +000063 return ''
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000064 elif len(self.args) == 1:
65 return str(self.args[0])
66 else:
67 return str(self.args)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000068
69 def __getitem__(self, i):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000070 return self.args[i]
Barry Warsaw3e613ce1997-08-29 21:59:26 +000071
Fred Drake596db311997-10-06 15:48:20 +000072class StandardError(Exception):
73 pass
74
Barry Warsaw3e613ce1997-08-29 21:59:26 +000075class SyntaxError(StandardError):
Guido van Rossumf394f561997-09-05 19:00:56 +000076 filename = lineno = offset = text = None
Guido van Rossumc56ba381997-09-16 18:42:04 +000077 msg = ""
78 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000079 self.args = args
80 if len(self.args) >= 1:
81 self.msg = self.args[0]
82 if len(self.args) == 2:
83 info = self.args[1]
84 try:
85 self.filename, self.lineno, self.offset, self.text = info
86 except:
87 pass
Barry Warsaw3e613ce1997-08-29 21:59:26 +000088 def __str__(self):
Guido van Rossumf394f561997-09-05 19:00:56 +000089 return str(self.msg)
Barry Warsaw3e613ce1997-08-29 21:59:26 +000090
Barry Warsaw2dfe4de1998-07-23 16:03:46 +000091class EnvironmentError(StandardError):
92 """Base class for exceptions that occur outside the Python system.
93 Primarily used as a base class for OSError and IOError."""
Barry Warsaw3e613ce1997-08-29 21:59:26 +000094 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000095 self.args = args
Barry Warsaw3e613ce1997-08-29 21:59:26 +000096 self.errno = None
97 self.strerror = None
Barry Warsaw2dfe4de1998-07-23 16:03:46 +000098 self.filename = None
99 if len(args) == 3:
100 # open() errors give third argument which is the filename. BUT,
101 # so common in-place unpacking doesn't break, e.g.:
102 #
103 # except IOError, (errno, strerror):
104 #
105 # we hack args so that it only contains two items. This also
106 # means we need our own __str__() which prints out the filename
107 # when it was supplied.
108 self.errno, self.strerror, self.filename = args
109 self.args = args[0:2]
Guido van Rossumc56ba381997-09-16 18:42:04 +0000110 if len(args) == 2:
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000111 # common case: PyErr_SetFromErrno()
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000112 self.errno, self.strerror = args
113
114 def __str__(self):
Guido van Rossumbe21d981998-08-11 18:01:32 +0000115 if self.filename is not None:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000116 return '[Errno %s] %s: %s' % (self.errno, self.strerror,
Guido van Rossumbe21d981998-08-11 18:01:32 +0000117 repr(self.filename))
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000118 elif self.errno and self.strerror:
Jeremy Hyltonec8c8c21998-07-28 17:30:06 +0000119 return '[Errno %s] %s' % (self.errno, self.strerror)
Barry Warsaw2dfe4de1998-07-23 16:03:46 +0000120 else:
121 return StandardError.__str__(self)
122
123class IOError(EnvironmentError):
124 pass
125
126class OSError(EnvironmentError):
127 """Used by the posix module."""
128 pass
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000129
130class RuntimeError(StandardError):
131 pass
132
133class SystemError(StandardError):
134 pass
135
136class EOFError(StandardError):
137 pass
138
139class ImportError(StandardError):
140 pass
141
142class TypeError(StandardError):
143 pass
144
145class ValueError(StandardError):
146 pass
147
148class KeyboardInterrupt(StandardError):
149 pass
150
151class AssertionError(StandardError):
152 pass
153
Barry Warsaw25131fa1997-09-16 21:50:59 +0000154class ArithmeticError(StandardError):
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000155 pass
156
Barry Warsaw25131fa1997-09-16 21:50:59 +0000157class OverflowError(ArithmeticError):
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000158 pass
159
Barry Warsaw25131fa1997-09-16 21:50:59 +0000160class FloatingPointError(ArithmeticError):
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000161 pass
162
Barry Warsaw25131fa1997-09-16 21:50:59 +0000163class ZeroDivisionError(ArithmeticError):
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000164 pass
165
166class LookupError(StandardError):
167 pass
168
169class IndexError(LookupError):
170 pass
171
172class KeyError(LookupError):
173 pass
174
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000175class AttributeError(StandardError):
176 pass
177
178class NameError(StandardError):
179 pass
180
Barry Warsaw3e613ce1997-08-29 21:59:26 +0000181class MemoryError(StandardError):
182 pass
Guido van Rossumc56ba381997-09-16 18:42:04 +0000183
184class SystemExit(Exception):
185 def __init__(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000186 self.args = args
Guido van Rossumc56ba381997-09-16 18:42:04 +0000187 if len(args) == 0:
188 self.code = None
189 elif len(args) == 1:
190 self.code = args[0]
191 else:
192 self.code = args