How can I print a traceback without raising an exception?

Discussion in 'Python' started by Gerald Britton, Feb 13, 2011.

  1. In the following example, I raise an exception in function h() which
    prints a traceback. I would like to know how I can get a similar
    display on my terminal without raising an exception. That is, can I
    replace "raise Exception" in function h() with some sequence of
    instructions (possibly using the inspect module) that will generate a
    similar call trace of how I got to that point?

    Example:

    >>> def f():

    .... g()
    ....
    >>> def g():

    .... h()
    ....
    >>> def h():

    .... raise Exception # Replace with something to generate a call trace
    ....
    >>> f()

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<stdin>", line 2, in f
    File "<stdin>", line 2, in g
    File "<stdin>", line 2, in h
    Exception
    >>>



    --
    Gerald Britton
     
    Gerald Britton, Feb 13, 2011
    #1
    1. Advertisements

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. Jan Decaluwe
    Replies:
    0
    Views:
    429
    Jan Decaluwe
    Nov 7, 2003
  2. Saizan
    Replies:
    5
    Views:
    477
    Fabio Zadrozny
    Feb 2, 2006
  3. Tamer Ibrahim
    Replies:
    3
    Views:
    550
    Alexey Smirnov
    Jan 29, 2007
  4. keto
    Replies:
    0
    Views:
    1,361
  5. Replies:
    1
    Views:
    313
    Gabriel Genellina
    Oct 16, 2007
  6. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    634
    David Cournapeau
    Dec 30, 2008
  7. Jack Bates
    Replies:
    0
    Views:
    439
    Jack Bates
    May 2, 2011
  8. Îίκος
    Replies:
    104
    Views:
    964
    Mark Lawrence
    Sep 30, 2013
Loading...