python try語句如何列印錯誤行
阿新 • • 發佈:2019-02-15
列印當前.py檔案錯誤行:
import sys
try:
a = [1,2]
print a[3]
except:
s=sys.exc_info()
print "Error '%s' happened on line %d" %
(s[1],s[2].tb_lineno)
列印execfile的列印錯誤行:
try:
execfile("tprint.py")
except Exception, info:
#print info[1]
print "Error '%s' happened on line %d" %
(info[0], info[1][1])
利用反射機制,呼叫函式,列印被呼叫方的錯誤行及錯誤資訊
try:
callfunc.callfunc(myklass,strmethod,params)
except :
print '=== STEP ERROR INFO START'
import traceback
traceback.print_exc()
print '=== STEP ERROR INFO END'