howto catch an Exception and still print the TraceBack?

Discussion in 'Python' started by Saizan, Feb 1, 2006.

  1. Saizan

    Saizan Guest

    In an event-driven application i'd like to keep the program alive regardless of any exceptions raised by the handlers,
    but still be able to debug them by reading the appropriate TraceBack from stderr.
    I can put something like:

    try:
    self.call_handler(handler,*args)
    except Exception, e:
    print e
    print e.args

    in the dispatcher, but that isn't as helpful as a complete TraceBack.
    Saizan, Feb 1, 2006
    #1
    1. Advertising

  2. Op 2006-02-01, Saizan schreef <>:
    > In an event-driven application i'd like to keep the program alive regardless of any exceptions raised by the handlers,
    > but still be able to debug them by reading the appropriate TraceBack from stderr.
    > I can put something like:
    >
    > try:
    > self.call_handler(handler,*args)
    > except Exception, e:
    > print e
    > print e.args
    >
    > in the dispatcher, but that isn't as helpful as a complete TraceBack.


    You mean something like this?

    import traceback
    import sys

    try:
    self.call_handler(handler,*args)
    except Exception, e:
    Do_whatever_you need_to_do()
    for msg in traceback.format_tb(sys.exc_info()[2]):
    sys.stderr.write("%s\n" % msg)
    Antoon Pardon, Feb 1, 2006
    #2
    1. Advertising

  3. Saizan wrote:

    > In an event-driven application i'd like to keep the program alive
    > regardless of any exceptions raised by the handlers, but still be able to
    > debug them by reading the appropriate TraceBack from stderr. I can put
    > something like:


    See

    sys.exc_info()

    The you can do:

    try:
    ....
    except: # catch all
    _, e, tb = sys.exc_info()
    print tb


    Regards,

    Diez
    Diez B. Roggisch, Feb 1, 2006
    #3
  4. I find the following very good for most needs:

    try:
    raise RuntimeError('err')
    except:
    import traceback;traceback.print_exc()

    -- if you use Pydev, there's a template for that called printexc.

    Cheers,

    Fabio

    Saizan wrote:

    >In an event-driven application i'd like to keep the program alive regardless of any exceptions raised by the handlers,
    >but still be able to debug them by reading the appropriate TraceBack from stderr.
    >I can put something like:
    >
    >try:
    > self.call_handler(handler,*args)
    >except Exception, e:
    > print e
    > print e.args
    >
    >in the dispatcher, but that isn't as helpful as a complete TraceBack.
    >
    >
    Fabio Zadrozny, Feb 1, 2006
    #4
  5. Saizan

    Saizan Guest

    Thanks, I had completely missed the module traceback...
    I'll use traceback.print_exc(), it seems the most straightforward way.
    The only flaw is that the traceback starts in the method where i catch the exception and not from "__main__", but I guess it can't be helped.
    Saizan, Feb 2, 2006
    #5
  6. Saizan wrote:

    >Thanks, I had completely missed the module traceback...
    >I'll use traceback.print_exc(), it seems the most straightforward way.
    >The only flaw is that the traceback starts in the method where i catch the exception and not from "__main__", but I guess it can't be helped.
    >
    >

    Actually, I guess that if you wanted to check the 'upper stack', you
    could do it by checking sys._getframe() to get the current frame and
    then go upwards with frame.f_back (that's how debbugers work), that way
    you could get info on all the stacks you currently have... so if you
    think it's worth it... ;-P

    Cheers,

    Fabio
    Fabio Zadrozny, Feb 2, 2006
    #6
    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. dan
    Replies:
    2
    Views:
    6,009
  2. John Black
    Replies:
    8
    Views:
    4,136
    Xenos
    Aug 20, 2004
  3. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    546
    Daniel Pitts
    Aug 5, 2009
  4. Gerald Britton
    Replies:
    0
    Views:
    215
    Gerald Britton
    Feb 13, 2011
  5. Jack Bates
    Replies:
    0
    Views:
    263
    Jack Bates
    May 2, 2011
Loading...

Share This Page