Quick update to the README file. For intros and books we now point to
[python/dscho.git] / Lib / exceptions.py
blob43d1c2da40f34863c40371054537601786dc9ad1
1 """Class based built-in exception hierarchy.
3 New with Python 1.5, all standard built-in exceptions are now class objects by
4 default. This gives Python's exception handling mechanism a more
5 object-oriented feel. Traditionally they were string objects. Python will
6 fallback to string based exceptions if the interpreter is invoked with the -X
7 option, or if some failure occurs during class exception initialization (in
8 this case a warning will be printed).
10 Most existing code should continue to work with class based exceptions. Some
11 tricky uses of IOError may break, but the most common uses should work.
13 Here is a rundown of the class hierarchy. You can change this by editing this
14 file, but it isn't recommended because the old string based exceptions won't
15 be kept in sync. The class names described here are expected to be found by
16 the bltinmodule.c file. If you add classes here, you must modify
17 bltinmodule.c or the exceptions won't be available in the __builtin__ module,
18 nor will they be accessible from C.
20 The classes with a `*' are new since Python 1.5. They are defined as tuples
21 containing the derived exceptions when string-based exceptions are used. If
22 you define your own class based exceptions, they should be derived from
23 Exception.
25 Exception(*)
27 +-- SystemExit
28 +-- StandardError(*)
30 +-- KeyboardInterrupt
31 +-- ImportError
32 +-- EnvironmentError(*)
33 | |
34 | +-- IOError
35 | +-- OSError(*)
36 | |
37 | +-- WindowsError(*)
39 +-- EOFError
40 +-- RuntimeError
41 | |
42 | +-- NotImplementedError(*)
44 +-- NameError
45 | |
46 | +-- UnboundLocalError(*)
48 +-- AttributeError
49 +-- SyntaxError
50 +-- TypeError
51 +-- AssertionError
52 +-- LookupError(*)
53 | |
54 | +-- IndexError
55 | +-- KeyError
57 +-- ArithmeticError(*)
58 | |
59 | +-- OverflowError
60 | +-- ZeroDivisionError
61 | +-- FloatingPointError
63 +-- ValueError
64 | |
65 | +-- UnicodeError(*)
67 +-- SystemError
68 +-- MemoryError
69 """
71 class Exception:
72 """Proposed base class for all exceptions."""
73 def __init__(self, *args):
74 self.args = args
76 def __str__(self):
77 if not self.args:
78 return ''
79 elif len(self.args) == 1:
80 return str(self.args[0])
81 else:
82 return str(self.args)
84 def __getitem__(self, i):
85 return self.args[i]
87 class StandardError(Exception):
88 """Base class for all standard Python exceptions."""
89 pass
91 class SyntaxError(StandardError):
92 """Invalid syntax."""
93 filename = lineno = offset = text = None
94 msg = ""
95 def __init__(self, *args):
96 self.args = args
97 if len(self.args) >= 1:
98 self.msg = self.args[0]
99 if len(self.args) == 2:
100 info = self.args[1]
101 try:
102 self.filename, self.lineno, self.offset, self.text = info
103 except:
104 pass
105 def __str__(self):
106 return str(self.msg)
108 class EnvironmentError(StandardError):
109 """Base class for I/O related errors."""
110 def __init__(self, *args):
111 self.args = args
112 self.errno = None
113 self.strerror = None
114 self.filename = None
115 if len(args) == 3:
116 # open() errors give third argument which is the filename. BUT,
117 # so common in-place unpacking doesn't break, e.g.:
119 # except IOError, (errno, strerror):
121 # we hack args so that it only contains two items. This also
122 # means we need our own __str__() which prints out the filename
123 # when it was supplied.
124 self.errno, self.strerror, self.filename = args
125 self.args = args[0:2]
126 if len(args) == 2:
127 # common case: PyErr_SetFromErrno()
128 self.errno, self.strerror = args
130 def __str__(self):
131 if self.filename is not None:
132 return '[Errno %s] %s: %s' % (self.errno, self.strerror,
133 repr(self.filename))
134 elif self.errno and self.strerror:
135 return '[Errno %s] %s' % (self.errno, self.strerror)
136 else:
137 return StandardError.__str__(self)
139 class IOError(EnvironmentError):
140 """I/O operation failed."""
141 pass
143 class OSError(EnvironmentError):
144 """OS system call failed."""
145 pass
147 class WindowsError(OSError):
148 """MS-Windows OS system call failed."""
149 pass
151 class RuntimeError(StandardError):
152 """Unspecified run-time error."""
153 pass
155 class NotImplementedError(RuntimeError):
156 """Method or function hasn't been implemented yet."""
157 pass
159 class SystemError(StandardError):
160 """Internal error in the Python interpreter.
162 Please report this to the Python maintainer, along with the traceback,
163 the Python version, and the hardware/OS platform and version."""
164 pass
166 class EOFError(StandardError):
167 """Read beyond end of file."""
168 pass
170 class ImportError(StandardError):
171 """Import can't find module, or can't find name in module."""
172 pass
174 class TypeError(StandardError):
175 """Inappropriate argument type."""
176 pass
178 class ValueError(StandardError):
179 """Inappropriate argument value (of correct type)."""
180 pass
182 class KeyboardInterrupt(StandardError):
183 """Program interrupted by user."""
184 pass
186 class AssertionError(StandardError):
187 """Assertion failed."""
188 pass
190 class ArithmeticError(StandardError):
191 """Base class for arithmetic errors."""
192 pass
194 class OverflowError(ArithmeticError):
195 """Result too large to be represented."""
196 pass
198 class FloatingPointError(ArithmeticError):
199 """Floating point operation failed."""
200 pass
202 class ZeroDivisionError(ArithmeticError):
203 """Second argument to a division or modulo operation was zero."""
204 pass
206 class LookupError(StandardError):
207 """Base class for lookup errors."""
208 pass
210 class IndexError(LookupError):
211 """Sequence index out of range."""
212 pass
214 class KeyError(LookupError):
215 """Mapping key not found."""
216 pass
218 class AttributeError(StandardError):
219 """Attribute not found."""
220 pass
222 class NameError(StandardError):
223 """Name not found globally."""
224 pass
226 class UnboundLocalError(NameError):
227 """Local name referenced but not bound to a value."""
228 pass
230 class UnicodeError(ValueError):
231 """Unicode related error."""
232 pass
234 class MemoryError(StandardError):
235 """Out of memory."""
236 pass
238 class SystemExit(Exception):
239 """Request to exit from the interpreter."""
240 def __init__(self, *args):
241 self.args = args
242 if len(args) == 0:
243 self.code = None
244 elif len(args) == 1:
245 self.code = args[0]
246 else:
247 self.code = args