1997-08-29 18:59:26 -03:00
|
|
|
"""Class based built-in exception hierarchy.
|
|
|
|
|
1997-09-07 23:47:46 -03:00
|
|
|
This is a new feature whereby all the standard built-in exceptions,
|
|
|
|
traditionally string objects, are replaced with classes. This gives
|
|
|
|
Python's exception handling mechanism a more object-oriented feel.
|
1997-08-29 18:59:26 -03:00
|
|
|
|
|
|
|
Most existing code should continue to work with class based
|
|
|
|
exceptions. Some tricky uses of IOError may break, but the most
|
|
|
|
common uses should work.
|
|
|
|
|
1997-09-07 23:47:46 -03:00
|
|
|
To disable this feature, start the Python executable with the -X option.
|
1997-08-29 18:59:26 -03:00
|
|
|
|
|
|
|
Here is a rundown of the class hierarchy. You can change this by
|
|
|
|
editing this file, but it isn't recommended. The classes with a `*'
|
1997-09-07 23:47:46 -03:00
|
|
|
are new with this feature. They are defined as tuples containing the
|
|
|
|
derived exceptions when string-based exceptions are used.
|
1997-08-29 18:59:26 -03:00
|
|
|
|
1997-09-16 15:42:04 -03:00
|
|
|
Exception(*)
|
1997-08-29 18:59:26 -03:00
|
|
|
|
|
1997-09-16 15:42:04 -03:00
|
|
|
+-- StandardError(*)
|
|
|
|
|
|
|
|
|
+-- SystemExit
|
|
|
|
+-- KeyboardInterrupt
|
|
|
|
+-- ImportError
|
|
|
|
+-- IOError
|
|
|
|
+-- EOFError
|
|
|
|
+-- RuntimeError
|
|
|
|
+-- NameError
|
|
|
|
+-- AttributeError
|
|
|
|
+-- SyntaxError
|
|
|
|
+-- TypeError
|
|
|
|
+-- AssertionError
|
|
|
|
+-- LookupError(*)
|
|
|
|
| |
|
|
|
|
| +-- IndexError
|
|
|
|
| +-- KeyError
|
|
|
|
|
|
1997-09-16 18:50:59 -03:00
|
|
|
+-- ArithmeticError(*)
|
1997-09-16 15:42:04 -03:00
|
|
|
| |
|
|
|
|
| +-- OverflowError
|
|
|
|
| +-- ZeroDivisionError
|
|
|
|
| +-- FloatingPointError
|
|
|
|
|
|
|
|
|
+-- ValueError
|
|
|
|
+-- SystemError
|
|
|
|
+-- MemoryError
|
1997-08-29 18:59:26 -03:00
|
|
|
"""
|
|
|
|
|
1997-09-16 15:42:04 -03:00
|
|
|
class Exception:
|
1997-08-29 18:59:26 -03:00
|
|
|
def __init__(self, *args):
|
1997-09-16 15:42:04 -03:00
|
|
|
self.args = args
|
1997-08-29 18:59:26 -03:00
|
|
|
|
|
|
|
def __str__(self):
|
1997-09-16 15:42:04 -03:00
|
|
|
if not self.args:
|
1997-08-29 18:59:26 -03:00
|
|
|
return ''
|
1997-09-16 15:42:04 -03:00
|
|
|
elif len(self.args) == 1:
|
|
|
|
return str(self.args[0])
|
1997-09-05 16:00:56 -03:00
|
|
|
else:
|
|
|
|
return str(self.args)
|
1997-08-29 18:59:26 -03:00
|
|
|
|
1997-09-16 15:42:04 -03:00
|
|
|
class StandardError(Exception):
|
1997-08-29 18:59:26 -03:00
|
|
|
def __getitem__(self, i):
|
1997-09-16 15:42:04 -03:00
|
|
|
return self.args[i]
|
1997-08-29 18:59:26 -03:00
|
|
|
|
|
|
|
class SyntaxError(StandardError):
|
1997-09-05 16:00:56 -03:00
|
|
|
filename = lineno = offset = text = None
|
1997-09-16 15:42:04 -03:00
|
|
|
msg = ""
|
|
|
|
def __init__(self, *args):
|
|
|
|
self.args = args
|
|
|
|
if len(self.args) >= 1:
|
|
|
|
self.msg = self.args[0]
|
|
|
|
if len(self.args) == 2:
|
|
|
|
info = self.args[1]
|
|
|
|
try:
|
|
|
|
self.filename, self.lineno, self.offset, self.text = info
|
|
|
|
except:
|
|
|
|
pass
|
1997-08-29 18:59:26 -03:00
|
|
|
def __str__(self):
|
1997-09-05 16:00:56 -03:00
|
|
|
return str(self.msg)
|
1997-08-29 18:59:26 -03:00
|
|
|
|
|
|
|
class IOError(StandardError):
|
|
|
|
def __init__(self, *args):
|
1997-09-16 15:42:04 -03:00
|
|
|
self.args = args
|
1997-08-29 18:59:26 -03:00
|
|
|
self.errno = None
|
|
|
|
self.strerror = None
|
1997-09-16 15:42:04 -03:00
|
|
|
if len(args) == 2:
|
1997-08-29 18:59:26 -03:00
|
|
|
# common case: PyErr_SetFromErrno()
|
|
|
|
self.errno = args[0]
|
|
|
|
self.strerror = args[1]
|
|
|
|
|
|
|
|
class RuntimeError(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class SystemError(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class EOFError(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class ImportError(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class TypeError(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class ValueError(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class KeyboardInterrupt(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class AssertionError(StandardError):
|
|
|
|
pass
|
|
|
|
|
1997-09-16 18:50:59 -03:00
|
|
|
class ArithmeticError(StandardError):
|
1997-08-29 18:59:26 -03:00
|
|
|
pass
|
|
|
|
|
1997-09-16 18:50:59 -03:00
|
|
|
class OverflowError(ArithmeticError):
|
1997-08-29 18:59:26 -03:00
|
|
|
pass
|
|
|
|
|
1997-09-16 18:50:59 -03:00
|
|
|
class FloatingPointError(ArithmeticError):
|
1997-08-29 18:59:26 -03:00
|
|
|
pass
|
|
|
|
|
1997-09-16 18:50:59 -03:00
|
|
|
class ZeroDivisionError(ArithmeticError):
|
1997-08-29 18:59:26 -03:00
|
|
|
pass
|
|
|
|
|
|
|
|
class LookupError(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class IndexError(LookupError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class KeyError(LookupError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class AttributeError(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class NameError(StandardError):
|
|
|
|
pass
|
|
|
|
|
1997-09-16 15:42:04 -03:00
|
|
|
class MemoryError(StandardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class SystemExit(Exception):
|
1997-08-29 18:59:26 -03:00
|
|
|
def __init__(self, *args):
|
1997-09-16 15:42:04 -03:00
|
|
|
self.args = args
|
1997-08-29 18:59:26 -03:00
|
|
|
if len(args) == 0:
|
1997-09-16 15:42:04 -03:00
|
|
|
self.code = None
|
1997-08-29 18:59:26 -03:00
|
|
|
elif len(args) == 1:
|
1997-09-16 15:42:04 -03:00
|
|
|
self.code = args[0]
|
1997-08-29 18:59:26 -03:00
|
|
|
else:
|
1997-09-16 15:42:04 -03:00
|
|
|
self.code = args
|