confusion around CustomException example in 'Python in a Nutshell'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • erick_bodine@comcast.net

    #1

    confusion around CustomException example in 'Python in a Nutshell'

    In Martinelli's Nutshell book in the Exceptions chapter there is an
    example of a custom exception class (pg.112) that I am trying to
    implement without success. The custom exception class example pulls
    sys.exc_info() into an attribute and I am assuming that the attribute
    would then contain the raised exception info in a tuple (admittedly I
    could be assuming erroneously).

    class LinuxDriverErro r(Exception):
    def __init__(self, *args):
    Exception.__ini t__(self, *args)
    self.message = args[0]
    self.wrapped_ex c = sys.exc_info()

    try:
    raise LinuxDriverErro r, "raising Cain!"
    except CustomException , error:
    print error.message
    print error.wrapped_e xc

    # just checking
    print "sys.exc_info() : ", sys.exc_info()

    If I run the above code I get the following output:

    Just raising Cain!
    wrapped_exc: (None, None, None)
    sys.exc_info(): (<class __main__.LinuxD riverError at 0xf6f774dc>,
    <__main__.Linux DriverError instance at 0xf6f74bec>, <traceback object
    at 0xf6f7193c>)

    I do not understand why the wrapped_exc attribute contains no objects.

    I am running 2.3.4 on a fedora core 3 box.

    thanks,

    erick

  • wittempj@hotmail.com

    #2
    Re: confusion around CustomException example in 'Python in a Nutshell'

    I don't know the mentioned book, but if you rewrite like:[color=blue][color=green][color=darkred]
    >>> import sys
    >>> class LinuxDriverErro r(Exception):[/color][/color][/color]
    .... def __init__(self, *args):
    .... Exception.__ini t__(self, *args)
    .... self.message = args[0]
    ....
    .... def __str__(self):
    .... return str(sys.exc_inf o())
    ....[color=blue][color=green][color=darkred]
    >>> def test():[/color][/color][/color]
    .... raise LinuxDriverErro r, "raising Cain!"
    ....[color=blue][color=green][color=darkred]
    >>> try:[/color][/color][/color]
    .... test()
    .... except LinuxDriverErro r, error:
    .... print error
    ....
    (<class __main__.LinuxD riverError at 0x401e305c>,
    <__main__.Linux DriverError instance at 0x401deb6c>, <traceback object
    at 0x401df20c>)[color=blue][color=green][color=darkred]
    >>> # just checking[/color][/color][/color]
    .... print "sys.exc_info() : ", sys.exc_info()

    This gets what you probably expect, the way you do it there is no
    traceback object when you print it

    Comment

    • Peter Hansen

      #3
      Re: confusion around CustomException example in 'Python in a Nutshell'

      erick_bodine@co mcast.net wrote:[color=blue]
      > In Martinelli's Nutshell book in the Exceptions chapter there is an
      > example of a custom exception class (pg.112) that I am trying to
      > implement without success. The custom exception class example pulls
      > sys.exc_info() into an attribute and I am assuming that the attribute
      > would then contain the raised exception info in a tuple (admittedly I
      > could be assuming erroneously).
      >
      > class LinuxDriverErro r(Exception):
      > def __init__(self, *args):
      > Exception.__ini t__(self, *args)
      > self.message = args[0]
      > self.wrapped_ex c = sys.exc_info()
      >
      > try:
      > raise LinuxDriverErro r, "raising Cain!"
      > except CustomException , error:
      > print error.message
      > print error.wrapped_e xc
      >
      > # just checking
      > print "sys.exc_info() : ", sys.exc_info()
      >
      > If I run the above code I get the following output:
      >
      > Just raising Cain!
      > wrapped_exc: (None, None, None)
      > sys.exc_info(): (<class __main__.LinuxD riverError at 0xf6f774dc>,
      > <__main__.Linux DriverError instance at 0xf6f74bec>, <traceback object
      > at 0xf6f7193c>)
      >
      > I do not understand why the wrapped_exc attribute contains no objects.[/color]

      Because at the time you create the exception there is
      no "current exception" as seen from the point of view
      of sys.exc_info().

      Given the way this class was written, it is clearly
      intended to be raised from within an "except" statement
      as a result of another exception having been caught.

      Try this instead:

      try:
      try:
      1/0
      except:
      raise LinuxDriverErro r, 'raising Cain!'
      except Exception, ex:
      print ex.wrapped_exc


      By the way, the code you show is probably not what you
      were actually running anyway... you caught a
      "CustomExceptio n" but the LinuxDriverErro r is not
      a subclass of that class so it wouldn't/shouldn't
      have been caught.

      -Peter

      Comment

      • erick_bodine@comcast.net

        #4
        Re: confusion around CustomException example in 'Python in a Nutshell'


        Peter Hansen wrote:[color=blue]
        > erick_bodine@co mcast.net wrote:[color=green]
        > > In Martinelli's Nutshell book in the Exceptions chapter there is an
        > > example of a custom exception class (pg.112) that I am trying to
        > > implement without success. The custom exception class example[/color][/color]
        pulls[color=blue][color=green]
        > > sys.exc_info() into an attribute and I am assuming that the[/color][/color]
        attribute[color=blue][color=green]
        > > would then contain the raised exception info in a tuple (admittedly[/color][/color]
        I[color=blue][color=green]
        > > could be assuming erroneously).
        > >
        > > class LinuxDriverErro r(Exception):
        > > def __init__(self, *args):
        > > Exception.__ini t__(self, *args)
        > > self.message = args[0]
        > > self.wrapped_ex c = sys.exc_info()
        > >
        > > try:
        > > raise LinuxDriverErro r, "raising Cain!"
        > > except CustomException , error:
        > > print error.message
        > > print error.wrapped_e xc
        > >
        > > # just checking
        > > print "sys.exc_info() : ", sys.exc_info()
        > >
        > > If I run the above code I get the following output:
        > >
        > > Just raising Cain!
        > > wrapped_exc: (None, None, None)
        > > sys.exc_info(): (<class __main__.LinuxD riverError at 0xf6f774dc>,
        > > <__main__.Linux DriverError instance at 0xf6f74bec>, <traceback[/color][/color]
        object[color=blue][color=green]
        > > at 0xf6f7193c>)
        > >
        > > I do not understand why the wrapped_exc attribute contains no[/color][/color]
        objects.[color=blue]
        >
        > Because at the time you create the exception there is
        > no "current exception" as seen from the point of view
        > of sys.exc_info().
        >
        > Given the way this class was written, it is clearly
        > intended to be raised from within an "except" statement
        > as a result of another exception having been caught.
        >
        > Try this instead:
        >
        > try:
        > try:
        > 1/0
        > except:
        > raise LinuxDriverErro r, 'raising Cain!'
        > except Exception, ex:
        > print ex.wrapped_exc
        >[/color]
        This clears the fog a little.[color=blue]
        >
        > By the way, the code you show is probably not what you
        > were actually running anyway... you caught a
        > "CustomExceptio n" but the LinuxDriverErro r is not
        > a subclass of that class so it wouldn't/shouldn't
        > have been caught.[/color]

        <classic keyboard fumbling> What I am trying to do is get information
        from a raised custom exception. I am catching the exception in a
        main() function but it was actually raised in a separate module
        function. It would be nice if I could print out where the exception
        was raised from (module.functio n name + line number).

        -------------------------
        def some_module_fun ction():
        if something_bad:
        raise LinuxDriverErro r, "raising.."
        ---------------------------

        def main():
        try:
        mymodule.some_m odule_function( )
        except LinuxDriverErro r, error:
        print error.message
        print error.from_wher e



        Thanks,

        --Erick

        [color=blue]
        >
        > -Peter[/color]

        Comment

        • Peter Hansen

          #5
          Re: confusion around CustomException example in 'Python in a Nutshell'

          erick_bodine@co mcast.net wrote:[color=blue]
          > <classic keyboard fumbling> What I am trying to do is get information
          > from a raised custom exception. I am catching the exception in a
          > main() function but it was actually raised in a separate module
          > function. It would be nice if I could print out where the exception
          > was raised from (module.functio n name + line number).[/color]

          Ah, good. Exactly what the useful functions in the
          'traceback' module are intended for, if I understand
          your meaning...

          If doing "traceback.prin t_exc()" is printing the
          information you want (in a traceback that has much
          more than what you say you want), then you can use
          the other functions in that module, and methods in
          the 'traceback' objects themselves (the third item
          in the tuple returned by sys.exc_info()) , will
          give you all you could ever want...

          Google can help you find examples of usage as well,
          in the archives for this group.

          -Peter

          Comment

          Working...