Getting the line where an exception is raised

Steve Holden sholden at holdenweb.com
Wed Oct 9 13:14:25 EDT 2002


"JB" <jb at yahoo.de> wrote ...
> How can I get the line number of the line that has raised an
> exception?
>

>>> import sys, traceback
>>> def f(x):
...   try:
...     raise x
...   except:
...     print "Line number was", traceback.tb_lineno(sys.exc_info()[2])
...     raise
...
>>> f(NotImplementedError)
Line number was 3
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in f
NotImplementedError
>>>

Hope this helps.
-----------------------------------------------------------------------
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Previous .sig file retired to                    www.homeforoldsigs.com
-----------------------------------------------------------------------






More information about the Python-list mailing list