我是靠谱客的博主 无心天空,最近开发中收集的这篇文章主要介绍python traceback安装_python之traceback,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

traceback 模块允许你在程序里打印异常的跟踪返回 (Traceback)信息

1.1 traceback.print_exc()

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()

Traceback (innermost last):

File "traceback-example-1.py", line 7, in ?

SyntaxError: example

1.2 traceback.extract_tb

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]

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

1.3 traceback.extract_stack

import sys,traceback,os

def lumberjack():

bright_side_of_death()

def bright_side_of_death():

print traceback.extract_stack()

return 0

if __name__ == '__main__':

lumberjack()

运行结果:

[('D:\train\python\sample\traceback\sample.py', 12, '', 'lumberjack()'), ('D:\train\python\sample\traceback\sample.py', 4, 'lumberjack', 'bright_side_of_death()'), ('D:\train\python\sample\traceback\sample.py', 8, 'bright_side_of_death', 'print traceback.extract_stack()')]

最后

以上就是无心天空为你收集整理的python traceback安装_python之traceback的全部内容,希望文章能够帮你解决python traceback安装_python之traceback所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部