==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. What's the difference between - (one hyphen) and — (two hyphens) in a command?

    bash中看到这样的命令, curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash - sudo apt-get install - ...

  2. eclipse启动优化文章集合

    1. eclipse启动优化,终于不那么卡了! http://www.cfei.net/archives/445

  3. ZH奶酪:Ubuntu客户端通过SSH方式远程登录Ubuntu服务器

    1.概述 传统的远程登录(telnet,rlogin)时不安全的,他们在网络上用明文传输口令和数据,SSH则是安全的,openssh提供两种级别的验证方式. (1)基于口令的安全验证:知道服务器的帐号 ...

  4. ZOJ 2319 Beautiful People

    LIS.先按S降序升序再按B降序排序(如果B不按降序排序的话就会覆盖掉正解),然后再对B用O(nlog(n))的LIS求解就可以了.用d数组标记每个元素在上升序列中的位置,然后根据d倒着找id就可以了 ...

  5. MySQL Cluster线上管理节点配置文件-数据节点32G内存

    网上的朋友提供的,仅供参考. [NDBD DEFAULT] NoOfReplicas= 2 DataMemory=20G IndexMemory=5G MaxNoOfConcurrentTransac ...

  6. Hibernate(十四)缓存

    一.什么是缓存 缓存是介于应用程序和永久必数据存储源之间,目的是为了降低应用程序直接读写永久必数据存储源的频率,从而提高运行性能 缓存通常是在内存中的如: Office中的Word.excel Hib ...

  7. java.io.IOException: Input/output error

    java.io.FileOutputStream.close0(Native Method)at java.io.FileOutputStream.close(FileOutputStream.jav ...

  8. FFmpeg源码结构图 - 编码

    ===================================================== FFmpeg的库函数源码分析文章列表: [架构图] FFmpeg源码结构图 - 解码 FFm ...

  9. 1z0-052 q209_5

    5: Your database is open and the LISTENER listener is running. The new DBA of the system stops the l ...

  10. oracle 插入表数据的4种方式

      1.往表中插入一整行数据 /*方法一*/ INSERT INTO 表名 VALUES(val1,val2,val3,...); /*方法二*/ '; 如: ,, FROM DUAL; 注意: 2. ...