Clarify portability and main program.
[python/dscho.git] / Lib / exceptions.py
blob9c733ce2e96ccfb4138187866e3a4042f285ae65
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. The class names described here are expected
15 to be found by the bltinmodule.c file.
17 The classes with a `*' are new as of Python 1.5. They are defined as tuples
18 containing the derived exceptions when string-based exceptions are used. If
19 you define your own class based exceptions, they should be derived from
20 Exception.
22 Exception(*)
24 +-- StandardError(*)
26 +-- SystemExit
27 +-- KeyboardInterrupt
28 +-- ImportError
29 +-- EnvironmentError(*)
30 | |
31 | +-- IOError
32 | +-- OSError(*)
34 +-- EOFError
35 +-- RuntimeError
36 +-- NameError
37 +-- AttributeError
38 +-- SyntaxError
39 +-- TypeError
40 +-- AssertionError
41 +-- LookupError(*)
42 | |
43 | +-- IndexError
44 | +-- KeyError
46 +-- ArithmeticError(*)
47 | |
48 | +-- OverflowError
49 | +-- ZeroDivisionError
50 | +-- FloatingPointError
52 +-- ValueError
53 +-- SystemError
54 +-- MemoryError
55 """
57 class Exception:
58 def __init__(self, *args):
59 self.args = args
61 def __str__(self):
62 if not self.args:
63 return ''
64 elif len(self.args) == 1:
65 return str(self.args[0])
66 else:
67 return str(self.args)
69 def __getitem__(self, i):
70 return self.args[i]
72 class StandardError(Exception):
73 pass
75 class SyntaxError(StandardError):
76 filename = lineno = offset = text = None
77 msg = ""
78 def __init__(self, *args):
79 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
88 def __str__(self):
89 return str(self.msg)
91 class EnvironmentError(StandardError):
92 """Base class for exceptions that occur outside the Python system.
93 Primarily used as a base class for OSError and IOError."""
94 def __init__(self, *args):
95 self.args = args
96 self.errno = None
97 self.strerror = None
98 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.:
103 # except IOError, (errno, strerror):
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]
110 if len(args) == 2:
111 # common case: PyErr_SetFromErrno()
112 self.errno, self.strerror = args
114 def __str__(self):
115 if self.filename is not None:
116 return '[Errno %s] %s: %s' % (self.errno, self.strerror,
117 repr(self.filename))
118 elif self.errno and self.strerror:
119 return '[Errno %s] %s' % (self.errno, self.strerror)
120 else:
121 return StandardError.__str__(self)
123 class IOError(EnvironmentError):
124 pass
126 class OSError(EnvironmentError):
127 """Used by the posix module."""
128 pass
130 class RuntimeError(StandardError):
131 pass
133 class SystemError(StandardError):
134 pass
136 class EOFError(StandardError):
137 pass
139 class ImportError(StandardError):
140 pass
142 class TypeError(StandardError):
143 pass
145 class ValueError(StandardError):
146 pass
148 class KeyboardInterrupt(StandardError):
149 pass
151 class AssertionError(StandardError):
152 pass
154 class ArithmeticError(StandardError):
155 pass
157 class OverflowError(ArithmeticError):
158 pass
160 class FloatingPointError(ArithmeticError):
161 pass
163 class ZeroDivisionError(ArithmeticError):
164 pass
166 class LookupError(StandardError):
167 pass
169 class IndexError(LookupError):
170 pass
172 class KeyError(LookupError):
173 pass
175 class AttributeError(StandardError):
176 pass
178 class NameError(StandardError):
179 pass
181 class MemoryError(StandardError):
182 pass
184 class SystemExit(Exception):
185 def __init__(self, *args):
186 self.args = args
187 if len(args) == 0:
188 self.code = None
189 elif len(args) == 1:
190 self.code = args[0]
191 else:
192 self.code = args