0

For example, is there anything that could be added to the following that could provide the line number of the error?

try:
    assert False
except Exception, e:
    # lineOfError = ?
    # print lineOfError
    print e
ballade4op52
  • 2,142
  • 5
  • 27
  • 42

1 Answers1

1

You can use the traceback module:

from sys import exc_info
from traceback import extract_tb

try:
    assert False
except Exception as e:
    print(extract_tb(exc_info()[2])[0][1])
    print(e)
zondo
  • 19,901
  • 8
  • 44
  • 83
  • The only difference I needed was an added [0] index in order to access the tuple containing the error information: `print(extract_tb(exc_info()[2])[0][1])` – ballade4op52 Apr 13 '16 at 23:52
  • @Phillip: Thanks for catching that. It's fixed now. – zondo Apr 14 '16 at 00:17