Revision 12 as of 2004-08-25 04:59:45

Clear message

Writing Exception Classes

Exception classes are not special, you just derive them from Exception:

   1 class HostNotFound(Exception):
   2     def __init__(self, host):
   3         self.host = host
   4         super(HostNotFound, self)('Host Not Found exception: missing %s' % host)

You may later write:

   1 try:
   2     raise HostNotFound("taoriver.net")
   3 except HostNotFound, exc:
   4     # Handle exception.
   5     print exc  # -> 'Host Not Found exception: missing taoriver.net'
   6     print exc.host  # -> 'taoriver.net'

See Also

HandlingExceptions, TracebackModule

Questions

Unable to edit the page? See the FrontPage for instructions.