==traceback 模块==

[Example 2-18 #eg-2-18] 展示了 ``traceback`` 模块允许你在程序里打印异常的跟踪返回
(Traceback)信息, 类似未捕获异常时解释器所做的. 如 [Example 2-18 #eg-2-18] 所示. ====Example 2-18. 使用 traceback 模块打印跟踪返回信息====[eg-2-18] ```
File: traceback-example-1.py # note! importing the traceback module messes up the
# exception state, so you better do that here and not
# in the exception handler
# 注意! 导入 traceback 会清理掉异常状态, 所以
# 最好别在异常处理代码中导入该模块
import traceback try:
raise SyntaxError, "example"
except:
traceback.print_exc() *B*Traceback (innermost last):
File "traceback-example-1.py", line 7, in ?
SyntaxError: example*b*
``` [Example 2-19 #eg-2-19] 使用 ``StringIO`` 模块将跟踪返回信息放在字符串中. ====Example 2-19. 使用 traceback 模块将跟踪返回信息复制到字符串====[eg-2-19] ```
File: traceback-example-2.py import traceback
import StringIO try:
raise IOError, "an i/o error occurred"
except:
fp = StringIO.StringIO()
traceback.print_exc(file=fp)
message = fp.getvalue() print "failure! the error was:", repr(message) *B*failure! the error was: 'Traceback (innermost last):\012 File
"traceback-example-2.py", line 5, in ?\012IOError: an i/o error
occurred\012'*b*
``` 你可以使用 ``extract_tb`` 函数格式化跟踪返回信息, 得到包含错误信息的列表,
如 [Example 2-20 #eg-2-20] 所示. ====Example 2-20. 使用 traceback Module 模块编码 Traceback 对象====[eg-2-20] ```
File: traceback-example-3.py import traceback
import sys def function():
raise IOError, "an i/o error occurred" try:
function()
except:
info = sys.exc_info()
for file, lineno, function, text in traceback.extract_tb(info[2]):
print file, "line", lineno, "in", function
print "=>", repr(text)
print "** %s: %s" % info[:2] *B*traceback-example-3.py line 8 in ?
=> 'function()'
traceback-example-3.py line 5 in function
=> 'raise IOError, "an i/o error occurred"'
** exceptions.IOError: an i/o error occurred*b*
```

python标准库介绍——24 traceback 模块详解的更多相关文章

  1. python标准库介绍——12 time 模块详解

    ==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...

  2. python标准库介绍——27 random 模块详解

    ==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...

  3. python标准库介绍——10 sys 模块详解

    ==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...

  4. python标准库介绍——30 code 模块详解

    ==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...

  5. python标准库介绍——16 shutil模块详解

    ``shutil`` 实用模块包含了一些用于复制文件和文件夹的函数. [Example 2-4 #eg-2-4] 中使用的 ``copy`` 函数使用和 Unix 下 ``cp`` 命令基本相同的方式 ...

  6. python标准库介绍——8 operator 模块详解

    ==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...

  7. python标准库介绍——36 popen2 模块详解

    ==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...

  8. python标准库介绍——28 md5 模块详解

    ==md5 模块== ``md5`` (Message-Digest Algorithm 5)模块用于计算信息密文(信息摘要). ``md5`` 算法计算一个强壮的128位密文. 这意味着如果两个字符 ...

  9. python标准库介绍——23 UserString 模块详解

    ==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...

随机推荐

  1. Gh0st整理资料1

    题首 Gh0st是一款开源的远程控制软件.界面友好,性能高效.网上流传很多版本,比如红狼,饭客,败笔,大灰狼版本以及多如牛毛的个人修改的如外星人,Drat等个人修改版本.但内核都是基于Gh0st3.6 ...

  2. WordPress 后台添加额外选项字段到常规设置页面

    有时候我们需要添加一些额外的设置选项到常规设置(后台 > 设置 > 常规)页面,下面是一个简单的范例: 直接添加到主题的 functions.php 即可:   /*** WordPres ...

  3. linux系统下调度数据库类型资源库中的kettle job

    已经存在kettle的一个资源库enfo,在目录/works/wxj下面有一个job (testmailsuccess.kjb)如何实现手工在kettle外部执行此job和让系统每天定时的调用此job ...

  4. wstring to wchar_t*

      If you want to convert from std::wstring to const WCHAR* (i.e. the returned pointer gives read-onl ...

  5. [Javascript] Understand Curry

    The act of currying can be described as taking a multivariate function and turning it into a series ...

  6. iOS开发系列课程预告

    近期在Mac和iOS上做开发,认为应该写一点东西分享给感兴趣的童鞋们.在此之前.以前有非常多同行们都在埋怨苹果Objective-C的复杂和难以上手,为此也有非常多人对今年(2014年)刚推出的Swi ...

  7. linux 文件查找,which,whereis,locate,find

    linux 文件查找,which,whereis,locate,find 一:which 主要用于查找可执行命令的所在位置: 如图,查找命令 ls的目录: 二:whereis 主要用于查找命令的帮助文 ...

  8. C# Console 运行之后最小化到状态栏

    static void Main(string[] args) { new ConsoleCtrl(); Console.Read(); } class ConsoleCtrl { [DllImpor ...

  9. 算法笔记_118:算法集训之结果填空题集二(Java)

     目录 1 欧拉与鸡蛋 2 巧排扑克牌 3 排座位 4 黄金队列 5 汉诺塔计数 6 猜生日 7 棋盘上的麦子 8 国庆星期日 9 找素数 10 填写算式 11 取字母组成串   1 欧拉与鸡蛋 大数 ...

  10. vue单文件(sfc)编译为js的流程

    1.流程 2.参考文章地址 https://segmentfault.com/a/1190000012336392 3.Vue框架的parseComponent https://github.com/ ...