Creating New Exceptions

New exceptions are defined as classes

     class NetworkError(Exception):
         def __init__(self,args=None):
              self.args = args
     
     # Raise a user defined exception
     def lookup(name):
         ...
         raise NetworkError, "Bad hostname"
     
     # Catch a user defined exception
     try:
         statements
     except NetworkError, e:
          print e.args
  • User defined exceptions should inherit from Exception (although not enforced).
<<< O'Reilly OSCON 2000, Introduction to Python, Slide 75
July 17, 2000, beazley@cs.uchicago.edu
>>>