blob: 9c733ce2e96ccfb4138187866e3a4042f285ae65 [file] [log] [blame]
Guido van Rossuma11cccc1997-10-06 20:19:59 +00001"""Class based built-in exception hierarchy.
2
Guido van Rossum64e736b1998-10-02 01:23:47 +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).
Guido van Rossuma11cccc1997-10-06 20:19:59 +00009
Guido van Rossum64e736b1998-10-02 01:23:47 +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.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000012
Guido van Rossum64e736b1998-10-02 01:23:47 +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.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000016
Guido van Rossum64e736b1998-10-02 01:23:47 +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.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000021
22Exception(*)
23 |
24 +-- StandardError(*)
25 |
26 +-- SystemExit
27 +-- KeyboardInterrupt
28 +-- ImportError
Guido van Rossum64e736b1998-10-02 01:23:47 +000029 +-- EnvironmentError(*)
30 | |
31 | +-- IOError
32 | +-- OSError(*)
33 |
Guido van Rossuma11cccc1997-10-06 20:19:59 +000034 +-- EOFError
35 +-- RuntimeError
36 +-- NameError
37 +-- AttributeError
38 +-- SyntaxError
39 +-- TypeError
40 +-- AssertionError
41 +-- LookupError(*)
42 | |
43 | +-- IndexError
44 | +-- KeyError
45 |
46 +-- ArithmeticError(*)
47 | |
48 | +-- OverflowError
49 | +-- ZeroDivisionError
50 | +-- FloatingPointError
51 |
52 +-- ValueError
53 +-- SystemError
54 +-- MemoryError
55"""
56
57class Exception:
58 def __init__(self, *args):
Guido van Rossum548703a1998-03-26 22:14:20 +000059 self.args = args
Guido van Rossuma11cccc1997-10-06 20:19:59 +000060
61 def __str__(self):
62 if not self.args:
63 return ''
Guido van Rossum548703a1998-03-26 22:14:20 +000064 elif len(self.args) == 1:
65 return str(self.args[0])
66 else:
67 return str(self.args)
Guido van Rossuma11cccc1997-10-06 20:19:59 +000068
69 def __getitem__(self, i):
Guido van Rossum548703a1998-03-26 22:14:20 +000070 return self.args[i]
Guido van Rossuma11cccc1997-10-06 20:19:59 +000071
72class StandardError(Exception):
73 pass
74
75class SyntaxError(StandardError):
76 filename = lineno = offset = text = None
77 msg = ""
78 def __init__(self, *args):
Guido van Rossum548703a1998-03-26 22:14:20 +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
Guido van Rossuma11cccc1997-10-06 20:19:59 +000088 def __str__(self):
89 return str(self.msg)
90
Guido van Rossume03c0501998-08-12 02:38:11 +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."""
Guido van Rossuma11cccc1997-10-06 20:19:59 +000094 def __init__(self, *args):
Guido van Rossum548703a1998-03-26 22:14:20 +000095 self.args = args
Guido van Rossuma11cccc1997-10-06 20:19:59 +000096 self.errno = None
97 self.strerror = None
Guido van Rossume03c0501998-08-12 02:38:11 +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 Rossuma11cccc1997-10-06 20:19:59 +0000110 if len(args) == 2:
111 # common case: PyErr_SetFromErrno()
Guido van Rossume03c0501998-08-12 02:38:11 +0000112 self.errno, self.strerror = args
113
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)
122
123class IOError(EnvironmentError):
124 pass
125
126class OSError(EnvironmentError):
127 """Used by the posix module."""
128 pass
Guido van Rossuma11cccc1997-10-06 20:19:59 +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
154class ArithmeticError(StandardError):
155 pass
156
157class OverflowError(ArithmeticError):
158 pass
159
160class FloatingPointError(ArithmeticError):
161 pass
162
163class ZeroDivisionError(ArithmeticError):
164 pass
165
166class LookupError(StandardError):
167 pass
168
169class IndexError(LookupError):
170 pass
171
172class KeyError(LookupError):
173 pass
174
175class AttributeError(StandardError):
176 pass
177
178class NameError(StandardError):
179 pass
180
181class MemoryError(StandardError):
182 pass
183
184class SystemExit(Exception):
185 def __init__(self, *args):
Guido van Rossum548703a1998-03-26 22:14:20 +0000186 self.args = args
Guido van Rossuma11cccc1997-10-06 20:19:59 +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