Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to recreate traceback?

Hello, Sage Community.

When we write something like sin(t) without previously defining the variable t, we get a traceback like

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-138-17829414d33a> in <module>()
----> 1 sin(t)

NameError: name 't' is not defined

I would like to reproduce this text automatically. I have tried the following:

try:
    sin(t)
except:
    _typ_, _val_, _tb_ = sys.exc_info()
    traceback.print_exception(_typ_, _val_, _tb_)

However, this produces a less rich output:

Traceback (most recent call last):
   File "<ipython-input-139-55b9ce98509c>", line 2, in <module>
      sin(t)
   NameError: name 't' is not defined

How could I reproduce the rich output?

Thanks in advance for your answers!