Reporting the line number of an exception

S

sophie_newbie

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?

Thanks for any help!
 
P

Peter Otten

sophie_newbie said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,250
Members
46,848
Latest member
Graciela Mitchell

Latest Threads

Top