import traceback
try:
1/0
except Exception,e:
traceback.print_exc()
输出结果是
Traceback (most recent call last):
File "test_traceback.py", line 3, in <module>
1/0
 
 
ZeroDivisionError: integer division or modulo by zero
这样非常直观有利于调试。
 
 
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文件去。

你也可以传入一个文件, 把返回信息写到文件中去, 如下:

1
2
3
4
5
6
7
8
9
import traceback
import StringIO
try:
    raise SyntaxError, "traceback test"
except:
    fp = StringIO.StringIO()    #创建内存文件对象
    traceback.print_exc(file=fp)
    message = fp.getvalue()
    print message

这样在控制台输出的结果和上面例子一样,traceback模块还提供了extract_tb函数格式化跟踪返回信息, 得到包含错误信息的列表, 如下:

1
2
3
4
5
6
7
8
9
10
11
12
import traceback
import sys
def tracebacktest():
    raise SyntaxError, "traceback test"
try:
    tracebacktest()
except:
    info = sys.exc_info()
    for file, lineno, function, text in traceback.extract_tb(info[2]):
        print file"line:", lineno, "in", function
        print text
    print "** %s: %s" % info[:2]

控制台输出结果如下:

1
2
3
4
5
H:PythonWorkSpaceTestsrcTracebackTest.py line: 7 in <module>
tracebacktest()
H:PythonWorkSpaceTestsrcTracebackTest.py line: 5 in tracebacktest
raise SyntaxError, "traceback test"
** <type 'exceptions.SyntaxError'>: traceback test

python的traceback模块的更多相关文章

  1. Python traceback 模块,追踪错误

    Python traceback 模块,追踪错误 import traceback try: your code except: traceback.print_exc()

  2. Python 进阶 之 traceback模块

    Traceback模块官方英文描述: Help on module traceback: NAME traceback - Extract, format and print information ...

  3. Python 处理异常栈模块——traceback 模块

    异常捕捉 通常我们在项目中,针对异常的捕捉会使用 try + except,基本形式如下: try: # 主代码 except IndexError as e: # 索引异常时执行这里 logger. ...

  4. Python traceback模块简单使用

    Python中的traceback模块被用于跟踪异常返回信息,可以在logging中记录下traceback. traceback.format_exc() 获取异常为字符串,保存到日志文件 try: ...

  5. python标准库介绍——24 traceback 模块详解

    ==traceback 模块== [Example 2-18 #eg-2-18] 展示了 ``traceback`` 模块允许你在程序里打印异常的跟踪返回 (Traceback)信息, 类似未捕获异常 ...

  6. Python中的traceback模块

    traceback模块被用来跟踪异常返回信息. 如下例所示: 1.直接打印异常信息 import traceback try: raise SyntaxError, "traceback t ...

  7. python - 标准库:traceback模块

    traceback 模块: 允许你在程序里打印异常的跟踪返回 (Traceback)信息, 类似未捕获异常时解释器所做的. import traceback try: raise SyntaxErro ...

  8. python基础——第三方模块

    python基础——第三方模块 在Python中,安装第三方模块,是通过包管理工具pip完成的.  如果你正在使用Mac或Linux,安装pip本身这个步骤就可以跳过了.  如果你正在使用Window ...

  9. python中string模块各属性以及函数的用法

    任何语言都离不开字符,那就会涉及对字符的操作,尤其是脚本语言更是频繁,不管是生产环境还是面试考验都要面对字符串的操作.     python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串 ...

随机推荐

  1. 使用开源库 SVPullToRefresh 实现上拉加载下拉刷新

    SVPullToRefresh开源库地址 https://github.com/samvermette/SVPullToRefresh 将整个文件夹SVPullToRefresh拖入工程中并引入头文件 ...

  2. 行尸走肉第八季/全集The Walking Dead迅雷下载

    <行尸走肉第八季>将在10月22号回归播出, 将于5月1日起正式开始拍摄!目前剧组已开始招募客串演员和丧尸群演,第八季拍摄时长初步预计会持续到11月16日.而这三位已确认为<行尸走肉 ...

  3. 【 D3.js 入门系列 --- 2 】 怎样使用数据和选择元素

    本人的个人博客首页为: http://www.ourd3js.com/  ,csdn博客首页为:http://blog.csdn.net/lzhlzz/. 转载请注明出处,谢谢. 接着上一讲的内容,这 ...

  4. C#零基础入门07:打老鼠之面向对象重构

    一:前言 有了上面两节的知识,尤其是第六节之后,现在我们回过头看我们的打老鼠游戏,我们是不是会发现:这个程序也太不面向对象了.我们所有的代码逻辑都分布在Code-Hide中(UI的后台代码,称之为Co ...

  5. Generalized normal distribution and Skew normal distribution

    Density Function The Generalized Gaussian density has the following form: where  (rho) is the " ...

  6. [转]Redis作者:深度剖析Redis持久化

    From : http://www.iteye.com/news/24675 Redis是一种面向“key-value”类型数据的分布式NoSQL数据库系统,具有高性能.持久存储.适应高并发应用场景等 ...

  7. 第二章 BIO与NIO

    <netty权威指南>读书笔记 一.BIO 1.服务端程序: package bio; import java.io.BufferedReader; import java.io.IOEx ...

  8. go语言之进阶篇接口转换

    1.go语音之进阶篇 示例: package main import "fmt" type Humaner interface { //子集 sayhi() } type Pers ...

  9. ds18b20驱动及应用程序

    ---------------------------------------------------------------------------------------------------- ...

  10. ubuntu下nodejs开发环境搭建

    1.安装nodejs sudo apt install -y nodejs 2.更新npm到最新版本 sudo npm i -g npm 3.npm配置为淘宝镜像 sudo npm config se ...