Size: 1196
Comment: Exceptions derive from exception. Need call parent initializer?
|
Size: 1014
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 3: | Line 3: |
I (LionKimbro) don't know much about writing exception classes; Here's hoping someone rewrites this better. Derive Exception classes from Exception: |
Exception classes are not special, you just derive them from Exception: |
Line 12: | Line 10: |
Exception.__init__(self, 'Host Not Found exception: missing %s' % host) | |
Line 13: | Line 12: |
''Do we call {{{Exception.__init__( self )}}}..?'' -- LionKimbro [[DateTime(2003-09-06T19:51:18Z)]] |
|
Line 23: | Line 20: |
print "Host Not Found:", X.host | # Handle exception. # "X" is the HostNotFound instance. |
Line 26: | Line 24: |
== Overloading __str__ == | == See Also == |
Line 28: | Line 26: |
You can overload {{{__str__}}} to get the exception to explain itself: {{{ #!python class HostNotFound(Exception): def __init__( self, host ): self.host = host def __str__( self ): return "Host Not Found exception: missing %s" % self.host }}} That way, you only need print the exception instance: {{{ #!python try: raise HostNotFound( "taoriver.net" ) except HostNotFound, X: print X }}} I don't know if this is a good idea or not. |
HandlingExceptions, TracebackModule |
Line 53: | Line 30: |
* How do you relay the traceback information? | * 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?'' * When you're logging exceptions, you want access to the traceback information to. After some research, I believe what you use is extract_tb or extract_stack from the traceback module. -- 216.254.10.130 [[DateTime(2003-09-07T15:23:43Z)]] |
Writing Exception Classes
Exception classes are not special, you just derive them from Exception:
You may later write:
See Also
HandlingExceptions, TracebackModule
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?
When you're logging exceptions, you want access to the traceback information to. After some research, I believe what you use is extract_tb or extract_stack from the traceback module. -- 216.254.10.130 DateTime(2003-09-07T15:23:43Z)
- What better exception-foo is out there?