home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Lib / exceptions.py < prev    next >
Text File  |  1997-10-06  |  3KB  |  156 lines

  1. """Class based built-in exception hierarchy.
  2.  
  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.
  6.  
  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.
  10.  
  11. To disable this feature, start the Python executable with the -X option.
  12.  
  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.
  17.  
  18. Exception(*)
  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.  
  49. class Exception:
  50.     def __init__(self, *args):
  51.     self.args = args
  52.  
  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)
  60.  
  61.     def __getitem__(self, i):
  62.     return self.args[i]
  63.  
  64. class StandardError(Exception):
  65.     pass
  66.  
  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)
  82.  
  83. class IOError(StandardError):
  84.     def __init__(self, *args):
  85.     self.args = args
  86.         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.  
  93. class RuntimeError(StandardError):
  94.     pass
  95.  
  96. class SystemError(StandardError):
  97.     pass
  98.  
  99. class EOFError(StandardError):
  100.     pass
  101.  
  102. class ImportError(StandardError):
  103.     pass
  104.  
  105. class TypeError(StandardError):
  106.     pass
  107.  
  108. class ValueError(StandardError):
  109.     pass
  110.  
  111. class KeyboardInterrupt(StandardError):
  112.     pass
  113.  
  114. class AssertionError(StandardError):
  115.     pass
  116.  
  117. class ArithmeticError(StandardError):
  118.     pass
  119.  
  120. class OverflowError(ArithmeticError):
  121.     pass
  122.  
  123. class FloatingPointError(ArithmeticError):
  124.     pass
  125.  
  126. class ZeroDivisionError(ArithmeticError):
  127.     pass
  128.  
  129. class LookupError(StandardError):
  130.     pass
  131.  
  132. class IndexError(LookupError):
  133.     pass
  134.  
  135. class KeyError(LookupError):
  136.     pass
  137.  
  138. class AttributeError(StandardError):
  139.     pass
  140.  
  141. class NameError(StandardError):
  142.     pass
  143.  
  144. class MemoryError(StandardError):
  145.     pass
  146.  
  147. class SystemExit(Exception):
  148.     def __init__(self, *args):
  149.     self.args = args
  150.         if len(args) == 0:
  151.             self.code = None
  152.         elif len(args) == 1:
  153.             self.code = args[0]
  154.         else:
  155.             self.code = args
  156.