import sys
import traceback
def fun0():
iX = 0
iY = 1
iZ = iY/iX
return iZ
def fun1():
try:
iA = 1
iB = fun0()
iC = iA + iB
except Exception,data:
e_type, e_value, e_tb = sys.exc_info()
msgList = traceback.extract_tb(e_tb)
print "Call Stack:" , len(msgList)
for filename, lineno, name, line in msgList:
print filename, lineno, name, line
return 1
运行结果:
E:/python>python
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import Demo
>>> Demo.fun1()
Call Stack: 2
Demo.py 13 fun1 iB = fun0()
Demo.py 7 fun0 iZ = iY/iX
1
>>>