Reporting the line number of an exception

Peter Otten __peter__ at web.de
Thu May 29 09:33:25 EDT 2008


sophie_newbie wrote:

> I'm sure this is exceedingly simple but I can't find it anywhere. When
> I catch an exception I would like to report the line number of the
> exception as well as the error info.
> 
> try:
>     someError()
> except Exception, e:
>     "print_error_and_line_number"
> 
> How do I find the line number?

If you want just the line number:

tb = sys.exc_info()[2]
print tb.tb_lineno

You may also have a look at the traceback module, e. g.:

traceback.print_exc()

Peter



More information about the Python-list mailing list