Writing Exception Classes
I (LionKimbro) don't know much about writing exception classes; Here's hoping someone rewrites this better.
Exception classes are not special, you just derive them from Exception:
Toggle line numbers
1 class HostNotFound(Exception):
2 def __init__( self, host ):
3 self.host = host
4 Exception.__init__(self)
You may later write:
Toggle line numbers
1 try:
2 raise HostNotFound( "taoriver.net" )
3 except HostNotFound, X:
4 print "Host Not Found:", X.host
Overloading __str__
You can overload __str__ to get the exception to explain itself:
That way, you only need print the exception instance:
Toggle line numbers
1 try:
2 raise HostNotFound( "taoriver.net" )
3 except HostNotFound, X:
4 print X
I don't know if this is a good idea or not.
In this instance, there's no need to overload __str__, as the standard Exception already has a __str__ method:
Toggle line numbers
1 class HostNotFound(Exception):
2 def __init__(self, host):
3 self.host = host
4 Exception.__init__(self, 'Host Not Found exception: missing %s' % host)
Questions
How do you relay the traceback information? Relay the traceback information? Moving it higher up the call-stack? Could you try to explain your question?
- What better exception-foo is out there?