Differences between revisions 1 and 12 (spanning 11 versions)
Revision 1 as of 2003-09-06 18:34:16
Size: 1097
Editor: dsl254-010-130
Comment: Initial version, Req for Comment.
Revision 12 as of 2004-08-25 04:59:45
Size: 1270
Editor: cpe-68-112-255-10
Comment: Updated example to use super()
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.

Exception classes are not special; You just write an ordinary class:
Exception classes are not special, you just derive them from Exception:
Line 9: Line 7:
class HostNotFound:
    def __init__( self, host ):
class HostNotFound(Exception):
    def __init__(self, host):
Line 12: Line 10:
        super(HostNotFound, self)('Host Not Found exception: missing %s' % host)
Line 19: Line 18:
    raise HostNotFound( "taoriver.net" )
except HostNotFound, X:
    print "Host Not Found:", X.host
    raise HostNotFound("taoriver.net")
except HostNotFound, exc:
    # Handle exception.
    print exc # -> '
Host Not Found exception: missing taoriver.net'
    print exc
.host  # -> 'taoriver.net'
    
Line 24: Line 26:
== Overloading __str__ == == See Also ==
Line 26: Line 28:
You can overload {{{__str__}}} to get the exception to explain itself:

{{{
#!python
class HostNotFound:
    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 51: Line 32:
  * 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. -- LionKimbro [[DateTime(2003-09-07T15:23:43Z)]]

  ''Look at cgitb for how to do detailed TB introspection, and as an example of why mixing logic and (HTML) layout is a very bad thing.''
Line 53: Line 38:

  ''AlexMartelli's "Dos and Don'ts".''

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

  • 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. -- LionKimbro DateTime(2003-09-07T15:23:43Z)

    Look at cgitb for how to do detailed TB introspection, and as an example of why mixing logic and (HTML) layout is a very bad thing.

  • What better exception-foo is out there?

    AlexMartelli's "Dos and Don'ts".

WritingExceptionClasses (last edited 2011-05-16 19:13:51 by VPN-18-101-8-113)

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