【发布时间】:2016-08-05 07:08:56
【问题描述】:
例如,是否可以在以下内容中添加任何可以提供错误行号的内容?
try:
assert False
except Exception, e:
# lineOfError = ?
# print lineOfError
print e
【问题讨论】:
例如,是否可以在以下内容中添加任何可以提供错误行号的内容?
try:
assert False
except Exception, e:
# lineOfError = ?
# print lineOfError
print e
【问题讨论】:
您可以使用traceback 模块:
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)
【讨论】:
print(extract_tb(exc_info()[2])[0][1])