python try語句如何打印錯誤行(含execfile)

打印當前.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'
相關文章
相關標籤/搜索