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. 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. Saizan
    Replies:
    5
    Views:
    334
    Fabio Zadrozny
    Feb 2, 2006
  2. Tamer Ibrahim
    Replies:
    3
    Views:
    426
    Alexey Smirnov
    Jan 29, 2007
  3. Replies:
    1
    Views:
    249
    Gabriel Genellina
    Oct 16, 2007
  4. Jack Bates
    Replies:
    0
    Views:
    279
    Jack Bates
    May 2, 2011
  5. Îίκος
    Replies:
    104
    Views:
    514
    Mark Lawrence
    Sep 30, 2013
Loading...

Share This Page