Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5013621
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: Python/Ruby

2011-05-31 15:57:36

traceback模块被用来跟踪异常返回信息. 如下例所示:
  1. import traceback
  2. try:
  3.     raise SyntaxError, "traceback test"
  4. except:
  5.     traceback.print_exc()
将会在控制台输出类似结果:
  1. Traceback (most recent call last):
  2. File "H:\PythonWorkSpace\Test\src\TracebackTest.py", line 3, in
  3. raise SyntaxError, "traceback test"
  4. SyntaxError: traceback test
 
 

类似在你没有捕获异常时候, 解释器所返回的结果.
你也可以传入一个文件, 把返回信息写到文件中去, 如下:
  1. import traceback
  2. import StringIO
  3. try:
  4.     raise SyntaxError, "traceback test"
  5. except:
  6.     fp = StringIO.StringIO() #创建内存文件对象
  7.     traceback.print_exc(file=fp)
  8.     message = fp.getvalue()
  9.     print message
这样在控制台输出的结果和上面例子一样
traceback模块还提供了extract_tb函数来格式化跟踪返回信息, 得到包含错误信息的列表, 如下:
  1. import traceback
  2. import sys

  3. def tracebacktest():
  4.     raise SyntaxError, "traceback test"
  5. try:
  6.     tracebacktest()
  7. except:
  8.     info = sys.exc_info()
  9.     for file, lineno, function, text in traceback.extract_tb(info[2]):
  10.         print file, "line:", lineno, "in", function
  11.         print text
  12.     print "** %s: %s" % info[:2]
 
控制台输出结果如下:
  1. H:\PythonWorkSpace\Test\src\TracebackTest.py line: 7 in
  2. tracebacktest()
  3. H:\PythonWorkSpace\Test\src\TracebackTest.py line: 5 in tracebacktest
  4. raise SyntaxError, "traceback test"
  5. ** : traceback test

 

Example 2-18 展示了 traceback 模块允许你在程序里打印异常的跟踪返回 (Traceback)信息, 类似未捕获异常时解释器所做的. 如 Example 2-18 所示. 2.11.0.1. Example 2-18. 使用 traceback 模块打印跟踪返回信息 File: traceback-example-1.py # note! import
Example 2-18 展示了 traceback 模块允许你在程序里打印异常的跟踪返回 (Traceback)信息, 类似未捕获异常时解释器所做的. 如 Example 2-18 所示.

2.11.0.1. Example 2-18. 使用 traceback 模块打印跟踪返回信息

 

  1. File: traceback-example-1.py

  2. # importing the traceback module messes up the
  3. # exception state, so you better do that here and not
  4. # in the exception handler
  5. # 导入 traceback 会清理掉异常状态, 所以
  6. # 最好别在异常处理代码中导入该模块
  7. import traceback

  8. try:
  9.     raise SyntaxError, "example"
  10. except:
  11.     traceback.print_exc()

 

  1. Traceback (innermost last):
  2. File "traceback-example-1.py", line 7, in ?
  3. SyntaxError: example

 

Example 2-19 使用 StringIO 模块将跟踪返回信息放在字符串中.

2.11.0.2. Example 2-19. 使用 traceback 模块将跟踪返回信息复制到字符串
File: traceback-example-2.py

 

  1. import traceback
  2. import StringIO

  3. try:
  4.     raise IOError, "an i/o error occurred"
  5. except:
  6.     fp = StringIO.StringIO()
  7.     traceback.print_exc(file=fp)
  8.     message = fp.getvalue()

  9.     print "failure! the error was:", repr(message)

 

  1. failure! the error was: 'Traceback (innermost last):\012 File
  2. "traceback-example-2.py", line 5, in ?\012IOError: an i/o error
  3. occurred\012'

 

你可以使用 extract_tb 函数格式化跟踪返回信息, 得到包含错误信息的列表, 如 Example 2-20 所示.

2.11.0.3. Example 2-20. 使用 traceback Module 模块编码 Traceback 对象
File: traceback-example-3.py

 

  1. import traceback
  2. import sys

  3. def function():
  4.     raise IOError, "an i/o error occurred"

  5. try:
  6.     function()
  7. except:
  8.     info = sys.exc_info()
  9.     for file, lineno, function, text in traceback.extract_tb(info[2]):
  10.         print file, "line", lineno, "in", function
  11.         print "=>", repr(text)
  12.     print "** %s: %s" % info[:2]

 

  1. traceback-example-3.py line 8 in ?
  2. => 'function()'
  3. traceback-example-3.py line 5 in function
  4. => 'raise IOError, "an i/o error occurred"'
  5. ** exceptions.IOError: an i/o error occurred

 

文章来自 http://docs.python.org/dev/library/traceback.html

阅读(2730) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

unknownmore2013-07-02 11:37:12

3q....