traceback模块用来跟踪异常返回信息
如下例所示:
import traceback
try:
raise SyntaxError, "traceback test"
except:
traceback.print_exc()
Traceback (most recent call last):
File "H:PythonWorkSpaceTestsrcTracebackTest.py", line 3, in <module>
raise SyntaxError, "traceback test"
SyntaxError: traceback test
你也可以传入一个文件, 把返回信息写到文件中去, 如下:
import traceback
import StringIO
try:
raise SyntaxError, "traceback test"
except:
fp = StringIO.StringIO() #创建内存文件对象
traceback.print_exc(file=fp)
message = fp.getvalue()
print message
常用方法及其区别
问题:traceback.print_exc()和traceback.format_exc()有什么区别呢?
format_exc()返回字符串,print_exc()则直接给打印出来。
即traceback.print_exc()与print traceback.format_exc()效果是一样的。
print_exc()还可以接受file参数直接写入到一个文件。比如
traceback.print_exc(file=open(‘tb.txt’,’w+’))写入到tb.txt文件去。
正文测试过程
sys.exc_info()
返回 (type, value, traceback). type为异常类型, value为异常的参数(通常为异常错误的信息), traceback为跟踪回溯的对象.
exc_type, exc_value, exc_traceback = sys.exc_info() print "*** print sys.exc_info:" print 'exc_type is: %s, exc_value is: %s, exc_traceback is: %s' % (exc_type, exc_value, exc_traceback)
输出:
*** print sys.exc_info: exc_type is: <type 'exceptions.IndexError'>, exc_value is: tuple index out of range, exc_traceback is: <traceback object at 0x7fee3b00eb48>
traceback.print_tb(traceback[, limit[, file]])
打印栈的跟踪信息. 如果省略limit, 将打印所有跟踪入口信息. file默认为std.err.
1
2
3
4
5
6
7
8
9
10
11
|
import traceback import sys def traceback_test(): try : raise SyntaxError, 'traceback_test' except Exception as e: traceback.print_tb(e, limit = 1 , file = sys.stdout) if __name__ = = '__main__' : traceback_test() |
错误信息
1
2
3
4
5
6
7
8
|
Traceback (most recent call last): File "D:/MH_code/test_flask_project/app/traceback.py" , line 14 , in <module> traceback_test() File "D:/MH_code/test_flask_project/app/traceback.py" , line 11 , in traceback_test traceback.print_tb(e, limit = 1 , file = sys.stdout) File "C:Python27lib raceback.py" , line 61 , in print_tb f = tb.tb_frame AttributeError: 'exceptions.SyntaxError' object has no attribute 'tb_frame' |
traceback.print_exception(type, value, traceback[, limit[, file]])
打印异常信息. (type, value, traceback)为sys.exc_info()返回的元组.和print_tb不同的是:
- 如果traceback不为空, 打印栈头信息(即最近被调用的信息).
- 在栈的信息后打印异常类型和参数.
- 如果是语法错误, 会打印对应的代码行数, 用”^”指明语法错误的位置.
1
2
3
4
5
6
7
8
9
10
11
12
|
import traceback import sys def traceback_test(): try : raise SyntaxError, 'traceback_test' except Exception as e:<br> exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback, limit = 2 , file = sys.stdout) if __name__ = = '__main__' : traceback_test() |
错误信息
1
|
None (用的 2.7 ,打印的什么玩意没细究) |
traceback.print_exc([limit[, file]])
print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, limit, file)的简写
1
2
3
4
5
6
7
8
9
10
11
|
import traceback import sys def traceback_test(): try : raise SyntaxError, 'traceback_test' except Exception as e: traceback.print_exc() if __name__ = = '__main__' : traceback_test() |
错误信息
1
2
3
4
|
Traceback (most recent call last): File "D:/MH_code/test_flask_project/app/traceback.py" , line 11 , in traceback_test raise SyntaxError, 'traceback_test' SyntaxError: traceback_test |
traceback.format_exc([limit])
类似于print_exc(limit), 但是返回字符串而不是输出到file.
1
2
3
4
5
6
7
8
9
10
11
12
|
import traceback import sys def traceback_test(): try : raise SyntaxError, 'traceback_test' except Exception as e: formatted_lines = traceback.format_exc().splitlines() print formatted_lines[ 0 ] print formatted_lines[ - 1 ] if __name__ = = '__main__' : traceback_test() |
错误信息
1
2
|
Traceback (most recent call last): SyntaxError: traceback_test |
traceback.format_exception(type, value, tb[, limit])
格式化栈信息和异常信息. 返回一个列表, 包括代码文件和代码行, 以及异常信息.
1
2
3
4
5
6
7
8
9
10
|
import traceback import sys def traceback_test(): try : raise SyntaxError, 'traceback_test' except Exception as e:<br> exc_type, exc_value, exc_traceback = sys.exc_info() print repr (traceback.format_exception(exc_type, exc_value, exc_traceback)) if __name__ = = '__main__' : traceback_test() |
错误信息
1
|
[ 'None
' ] |
traceback.extract_tb(traceback[, limit])
返回一个跟踪对象(traceback)的元组列表. 元组内容为(filename, line number, function name, text).
1
2
3
4
5
6
7
8
9
10
11
|
import traceback import sys def traceback_test(): try : raise SyntaxError, 'traceback_test' except Exception as e: exc_type, exc_value, exc_traceback = sys.exc_info() print repr (traceback.extract_tb(exc_traceback)) if __name__ = = '__main__' : traceback_test() |
错误信息
1
|
[( 'D:/MH_code/test_flask_project/app/traceback.py' , 11 , 'traceback_test' , "raise SyntaxError, 'traceback_test'" )] |
traceback.extract_stack([f[, limit]])
返回当前栈帧的原始跟踪(traceback)对象的信息, 格式和extract_tb一样, 元组内容为(filename, line number, function name, text).
1
2
3
4
5
6
7
8
9
10
|
import traceback import sys def traceback_test(): try : raise SyntaxError, 'traceback_test' except Exception as e: print traceback.extract_stack() if __name__ = = '__main__' : traceback_test() |
输出
1
2
3
|
[( 'D:/MH_code/test_flask_project/app/traceback.py' , 22 , '<module>' , 'traceback_test()' ), ( 'D:/MH_code/test_flask_project/app/traceback.py' , 20 , 'traceback_test' , ' print traceback.extract_stack()')] |
traceback.format_list(list)
按照list对应的项, 返回一个元组列表, 形式为同extract_tb()或者extract_stack()返回的一样. 元组内容为(filename, line number, function name, text)
将extract_tb()或者extract_stack()返回的list进行格式化.
traceback.format_tb(tb[, limit])
format_list(extract_tb(tb, limit))的简写.
traceback.format_stack([f[, limit]])
format_list(extract_stack(f, limit))的简写
traceback.tb_lineno(tb)
返回traceback对象设置的行数