Improved some error messages for command line processing.
[python/dscho.git] / Lib / dos-8x3 / exceptio.py
blob28711df63006628b5ea30d849f075cc5534254c3
1 """Class based built-in exception hierarchy.
3 This is a new feature whereby all the standard built-in exceptions,
4 traditionally string objects, are replaced with classes. This gives
5 Python's exception handling mechanism a more object-oriented feel.
7 Most existing code should continue to work with class based
8 exceptions. Some tricky uses of IOError may break, but the most
9 common uses should work.
11 To disable this feature, start the Python executable with the -X option.
13 Here is a rundown of the class hierarchy. You can change this by
14 editing this file, but it isn't recommended. The classes with a `*'
15 are new with this feature. They are defined as tuples containing the
16 derived exceptions when string-based exceptions are used.
18 Exception(*)
20 +-- StandardError(*)
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
38 +-- ArithmeticError(*)
39 | |
40 | +-- OverflowError
41 | +-- ZeroDivisionError
42 | +-- FloatingPointError
44 +-- ValueError
45 +-- SystemError
46 +-- MemoryError
47 """
49 class Exception:
50 def __init__(self, *args):
51 self.args = args
53 def __str__(self):
54 if not self.args:
55 return ''
56 elif len(self.args) == 1:
57 return str(self.args[0])
58 else:
59 return str(self.args)
61 def __getitem__(self, i):
62 return self.args[i]
64 class StandardError(Exception):
65 pass
67 class SyntaxError(StandardError):
68 filename = lineno = offset = text = None
69 msg = ""
70 def __init__(self, *args):
71 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
80 def __str__(self):
81 return str(self.msg)
83 class EnvironmentError(StandardError):
84 """Base class for exceptions that occur outside the Python system.
85 Primarily used as a base class for OSError and IOError."""
86 def __init__(self, *args):
87 self.args = args
88 self.errno = None
89 self.strerror = None
90 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.:
95 # except IOError, (errno, strerror):
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]
102 if len(args) == 2:
103 # common case: PyErr_SetFromErrno()
104 self.errno, self.strerror = args
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)
115 class IOError(EnvironmentError):
116 pass
118 class OSError(EnvironmentError):
119 """Used by the posix module."""
120 pass
122 class RuntimeError(StandardError):
123 pass
125 class SystemError(StandardError):
126 pass
128 class EOFError(StandardError):
129 pass
131 class ImportError(StandardError):
132 pass
134 class TypeError(StandardError):
135 pass
137 class ValueError(StandardError):
138 pass
140 class KeyboardInterrupt(StandardError):
141 pass
143 class AssertionError(StandardError):
144 pass
146 class ArithmeticError(StandardError):
147 pass
149 class OverflowError(ArithmeticError):
150 pass
152 class FloatingPointError(ArithmeticError):
153 pass
155 class ZeroDivisionError(ArithmeticError):
156 pass
158 class LookupError(StandardError):
159 pass
161 class IndexError(LookupError):
162 pass
164 class KeyError(LookupError):
165 pass
167 class AttributeError(StandardError):
168 pass
170 class NameError(StandardError):
171 pass
173 class MemoryError(StandardError):
174 pass
176 class SystemExit(Exception):
177 def __init__(self, *args):
178 self.args = args
179 if len(args) == 0:
180 self.code = None
181 elif len(args) == 1:
182 self.code = args[0]
183 else:
184 self.code = args