blob: ba63be6ecdfe26412f068f7aa5993e889087263d [file] [log] [blame]
Guido van Rossuma11cccc1997-10-06 20:19:59 +00001"""Class based built-in exception hierarchy.
2
3This is a new feature whereby all the standard built-in exceptions,
4traditionally string objects, are replaced with classes. This gives
5Python's exception handling mechanism a more object-oriented feel.
6
7Most existing code should continue to work with class based
8exceptions. Some tricky uses of IOError may break, but the most
9common uses should work.
10
11To disable this feature, start the Python executable with the -X option.
12
13Here is a rundown of the class hierarchy. You can change this by
14editing this file, but it isn't recommended. The classes with a `*'
15are new with this feature. They are defined as tuples containing the
16derived exceptions when string-based exceptions are used.
17
18Exception(*)
19 |
20 +-- StandardError(*)
21 |
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
37 |
38 +-- ArithmeticError(*)
39 | |
40 | +-- OverflowError
41 | +-- ZeroDivisionError
42 | +-- FloatingPointError
43 |
44 +-- ValueError
45 +-- SystemError
46 +-- MemoryError
47"""
48
49class Exception:
50 def __init__(self, *args):
Guido van Rossum548703a1998-03-26 22:14:20 +000051 self.args = args
Guido van Rossuma11cccc1997-10-06 20:19:59 +000052
53 def __str__(self):
54 if not self.args:
55 return ''
Guido van Rossum548703a1998-03-26 22:14:20 +000056 elif len(self.args) == 1:
57 return str(self.args[0])
58 else:
59 return str(self.args)
Guido van Rossuma11cccc1997-10-06 20:19:59 +000060
61 def __getitem__(self, i):
Guido van Rossum548703a1998-03-26 22:14:20 +000062 return self.args[i]
Guido van Rossuma11cccc1997-10-06 20:19:59 +000063
64class StandardError(Exception):
65 pass
66
67class SyntaxError(StandardError):
68 filename = lineno = offset = text = None
69 msg = ""
70 def __init__(self, *args):
Guido van Rossum548703a1998-03-26 22:14:20 +000071 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
Guido van Rossuma11cccc1997-10-06 20:19:59 +000080 def __str__(self):
81 return str(self.msg)
82
83class IOError(StandardError):
84 def __init__(self, *args):
Guido van Rossum548703a1998-03-26 22:14:20 +000085 self.args = args
Guido van Rossuma11cccc1997-10-06 20:19:59 +000086 self.errno = None
87 self.strerror = None
88 if len(args) == 2:
89 # common case: PyErr_SetFromErrno()
90 self.errno = args[0]
91 self.strerror = args[1]
92
93class RuntimeError(StandardError):
94 pass
95
96class SystemError(StandardError):
97 pass
98
99class EOFError(StandardError):
100 pass
101
102class ImportError(StandardError):
103 pass
104
105class TypeError(StandardError):
106 pass
107
108class ValueError(StandardError):
109 pass
110
111class KeyboardInterrupt(StandardError):
112 pass
113
114class AssertionError(StandardError):
115 pass
116
117class ArithmeticError(StandardError):
118 pass
119
120class OverflowError(ArithmeticError):
121 pass
122
123class FloatingPointError(ArithmeticError):
124 pass
125
126class ZeroDivisionError(ArithmeticError):
127 pass
128
129class LookupError(StandardError):
130 pass
131
132class IndexError(LookupError):
133 pass
134
135class KeyError(LookupError):
136 pass
137
138class AttributeError(StandardError):
139 pass
140
141class NameError(StandardError):
142 pass
143
144class MemoryError(StandardError):
145 pass
146
147class SystemExit(Exception):
148 def __init__(self, *args):
Guido van Rossum548703a1998-03-26 22:14:20 +0000149 self.args = args
Guido van Rossuma11cccc1997-10-06 20:19:59 +0000150 if len(args) == 0:
151 self.code = None
152 elif len(args) == 1:
153 self.code = args[0]
154 else:
155 self.code = args