confusion around CustomException example in 'Python in a Nutshell'

Discussion in 'Python' started by erick_bodine@comcast.net, Mar 11, 2005.

  1. Guest

    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 LinuxDriverError(Exception):
    def __init__(self, *args):
    Exception.__init__(self, *args)
    self.message = args[0]
    self.wrapped_exc = sys.exc_info()

    try:
    raise LinuxDriverError, "raising Cain!"
    except CustomException, error:
    print error.message
    print error.wrapped_exc

    # 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__.LinuxDriverError at 0xf6f774dc>,
    <__main__.LinuxDriverError 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
     
    , Mar 11, 2005
    #1
    1. Advertising

  2. Guest

    I don't know the mentioned book, but if you rewrite like:
    >>> import sys
    >>> class LinuxDriverError(Exception):

    .... def __init__(self, *args):
    .... Exception.__init__(self, *args)
    .... self.message = args[0]
    ....
    .... def __str__(self):
    .... return str(sys.exc_info())
    ....
    >>> def test():

    .... raise LinuxDriverError, "raising Cain!"
    ....
    >>> try:

    .... test()
    .... except LinuxDriverError, error:
    .... print error
    ....
    (<class __main__.LinuxDriverError at 0x401e305c>,
    <__main__.LinuxDriverError instance at 0x401deb6c>, <traceback object
    at 0x401df20c>)
    >>> # just checking

    .... 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
     
    , Mar 11, 2005
    #2
    1. Advertising

  3. Peter Hansen Guest

    wrote:
    > 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 LinuxDriverError(Exception):
    > def __init__(self, *args):
    > Exception.__init__(self, *args)
    > self.message = args[0]
    > self.wrapped_exc = sys.exc_info()
    >
    > try:
    > raise LinuxDriverError, "raising Cain!"
    > except CustomException, error:
    > print error.message
    > print error.wrapped_exc
    >
    > # 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__.LinuxDriverError at 0xf6f774dc>,
    > <__main__.LinuxDriverError instance at 0xf6f74bec>, <traceback object
    > at 0xf6f7193c>)
    >
    > I do not understand why the wrapped_exc attribute contains no objects.


    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 LinuxDriverError, '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
    "CustomException" but the LinuxDriverError is not
    a subclass of that class so it wouldn't/shouldn't
    have been caught.

    -Peter
     
    Peter Hansen, Mar 12, 2005
    #3
  4. Guest

    Peter Hansen wrote:
    > wrote:
    > > 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 LinuxDriverError(Exception):
    > > def __init__(self, *args):
    > > Exception.__init__(self, *args)
    > > self.message = args[0]
    > > self.wrapped_exc = sys.exc_info()
    > >
    > > try:
    > > raise LinuxDriverError, "raising Cain!"
    > > except CustomException, error:
    > > print error.message
    > > print error.wrapped_exc
    > >
    > > # 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__.LinuxDriverError at 0xf6f774dc>,
    > > <__main__.LinuxDriverError instance at 0xf6f74bec>, <traceback

    object
    > > at 0xf6f7193c>)
    > >
    > > I do not understand why the wrapped_exc attribute contains no

    objects.
    >
    > 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 LinuxDriverError, 'raising Cain!'
    > except Exception, ex:
    > print ex.wrapped_exc
    >

    This clears the fog a little.
    >
    > By the way, the code you show is probably not what you
    > were actually running anyway... you caught a
    > "CustomException" but the LinuxDriverError is not
    > a subclass of that class so it wouldn't/shouldn't
    > have been caught.


    <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.function name + line number).

    -------------------------
    def some_module_function():
    if something_bad:
    raise LinuxDriverError, "raising.."
    ---------------------------

    def main():
    try:
    mymodule.some_module_function()
    except LinuxDriverError, error:
    print error.message
    print error.from_where



    Thanks,

    --Erick


    >
    > -Peter
     
    , Mar 12, 2005
    #4
  5. Peter Hansen Guest

    wrote:
    > <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.function name + line number).


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

    If doing "traceback.print_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
     
    Peter Hansen, Mar 13, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Hansen
    Replies:
    1
    Views:
    336
    Robert Oschler
    Jul 11, 2003
  2. kbass

    OT: Python in a Nutshell

    kbass, May 30, 2004, in forum: Python
    Replies:
    2
    Views:
    275
    Tony C
    May 31, 2004
  3. Porky Pig Jr
    Replies:
    7
    Views:
    329
    Porky Pig Jr
    Jun 3, 2004
  4. Tuang
    Replies:
    14
    Views:
    484
    Alex Martelli
    Sep 26, 2004
  5. yps
    Replies:
    2
    Views:
    308
    Tommy Nordgren
    Jun 19, 2008
Loading...

Share This Page