python中利用tracekback跟踪栈以及打印异常信息

​##sys.exc_info() 
返回 (type, value, traceback). type为异常类型, value为异常的参数(通常为异常错误的信息), traceback为跟踪回溯的对象.

    exc_type, exc_value, exc_traceback = sys.exc_info()
    print "*** print sys.exc_info:"
    print 'exc_type is: %s, exc_value is: %s, exc_traceback is: %s' % (exc_type, exc_value, exc_traceback)

输出:

*** print sys.exc_info:
exc_type is: <type 'exceptions.IndexError'>, exc_value is: tuple index out of range, exc_traceback is: <traceback object at 0x7fee3b00eb48>

traceback.print_tb(traceback[, limit[, file]])

打印栈的跟踪信息. 如果省略limit, 将打印所有跟踪入口信息. file默认为std.err.

    print "*** print_tb:"
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)

输出:

*** print_tb:
  File "t.py", line 13, in <module>
    mock()

traceback.print_exception(type, value, traceback[, limit[, file]])

打印异常信息. (type, value, traceback)为sys.exc_info()返回的元组.和print_tb不同的是: 
- 如果traceback不为空, 打印栈头信息(即最近被调用的信息). 
- 在栈的信息后打印异常类型和常的参数. 
- 如果是语法错误, 会打印对应的代码行数, 用”^”指明语法错误的位置.

    print "*** print_exception:"
    traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)

输出:

*** print_exception:
Traceback (most recent call last):
  File "t.py", line 13, in <module>
    mock()
  File "t.py", line 4, in mock
    lumberjack()
IndexError: tuple index out of range

traceback.print_exc([limit[, file]])

print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, limit, file)的简写.

    print "*** print_exc:"
    traceback.print_exc()

输出:

*** print_exc:
Traceback (most recent call last):
  File "t.py", line 13, in <module>
    mock()
  File "t.py", line 4, in mock
    lumberjack()
  File "t.py", line 7, in lumberjack
    bright_side_of_death()
  File "t.py", line 10, in bright_side_of_death
    return tuple()[1]
IndexError: tuple index out of range

traceback.format_exc([limit])

类似于print_exc(limit), 但是返回字符串而不是输出到file.

    print "*** format_exc, first and last line:"
    formatted_lines = traceback.format_exc().splitlines()
    print formatted_lines[0]
    print formatted_lines[-1]

输出:

*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range

traceback.format_exception(type, value, tb[, limit])

格式化栈信息和异常信息. 返回一个列表, 包括代码文件和代码行, 以及异常信息.

    print "*** format_exception:"
    print repr(traceback.format_exception(exc_type, exc_value, exc_traceback))

输出:

['Traceback (most recent call last):\n', '  File "t.py", line 13, in <module>\n    mock()\n', '  File "t.py", line 10, in mock\n    lumberjack()\n', '  File "t.py", line 4, in lumberjack\n    bright_side_of_death()\n', '  File "t.py", line 7, in bright_side_of_death\n    return tuple()[0]\n', 'IndexError: tuple index out of range\n']

traceback.extract_tb(traceback[, limit])

返回一个跟踪对象(traceback)的元组列表. 元组内容为(filename, line number, function name, text).

    print "*** extract_tb:"
    print repr(traceback.extract_tb(exc_traceback))

输出:

[('t.py', 13, '<module>', 'mock()'), ('t.py', 4, 'mock', 'lumberjack()'), ('t.py', 7, 'lumberjack', 'bright_side_of_death()'), ('t.py', 10, 'bright_side_of_death', 'return tuple()[1]')]

traceback.extract_stack([f[, limit]])

返回当前栈帧的原始跟踪(traceback)对象的信息, 格式和extract_tb一样, 元组内容为(filename, line number, function name, text).

    print "*** extract_stack:"
    print traceback.extract_stack()

输出:

*** extract_stack:
[('t.py', 47, '<module>', 'print traceback.extract_stack()')]

traceback.format_list(list)

按照list对应的项, 返回一个元组列表, 形式为同extract_tb()或者extract_stack()返回的一样. 元组内容为(filename, line number, function name, text) 
将extract_tb()或者extract_stack()返回的list进行格式化.

print traceback.format_list([('spam.py', 3, '<module>', 'spam.eggs()'), ('eggs.py', 42, 'eggs', 'return "bacon"')])

输出:

['  File "spam.py", line 3, in <module>\n    spam.eggs()\n', '  File "eggs.py", line 42, in eggs\n    return "bacon"\n']

traceback.format_tb(tb[, limit])

format_list(extract_tb(tb, limit))的简写.

traceback.format_stack([f[, limit]])

format_list(extract_stack(f, limit))的简写

traceback.tb_lineno(tb)

返回traceback对象设置的行数

实例

import sys, traceback

def mock():
    lumberjack()

def lumberjack():
    bright_side_of_death()

def bright_side_of_death():
    return tuple()[1]

try:
    mock()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print "*** print sys.exc_info:"
    print 'exc_type is: %s, exc_value is: %s, exc_traceback is: %s' % (exc_type, exc_value, exc_traceback)
    print "-" *  100

    print "*** print_tb:"
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print "-" *  100

    print "*** print_exception:"
    traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
    print "-" *  100

    print "*** print_exc:"
    traceback.print_exc()
    print "-" *  100

    print "*** format_exc, first and last line:"
    formatted_lines = traceback.format_exc().splitlines()
    print formatted_lines[0]
    print formatted_lines[-1]
    print "-" *  100

    print "*** format_exception:"
    print repr(traceback.format_exception(exc_type, exc_value, exc_traceback))
    print "-" *  100

    print "*** extract_tb:"
    print repr(traceback.extract_tb(exc_traceback))
    print "-" *  100

    print "*** extract_stack:"
    print traceback.extract_stack()
    print "-" *  100

    print "*** format_tb:"
    print repr(traceback.format_tb(exc_traceback))
    print "-" *  100

    print "*** tb_lineno:", exc_traceback.tb_lineno

    print traceback.format_list([('spam.py', 3, '<module>', 'spam.eggs()'), ('eggs.py', 42, 'eggs', 'return "bacon"')])

输出:

*** print sys.exc_info:
exc_type is: <type 'exceptions.IndexError'>, exc_value is: tuple index out of range, exc_traceback is: <traceback object at 0x7f7d659bab48>
----------------------------------------------------------------------------------------------------
*** print_tb:
  File "t.py", line 13, in <module>
    mock()
----------------------------------------------------------------------------------------------------
*** print_exception:
Traceback (most recent call last):
  File "t.py", line 13, in <module>
    mock()
  File "t.py", line 4, in mock
    lumberjack()
IndexError: tuple index out of range
----------------------------------------------------------------------------------------------------
*** print_exc:
Traceback (most recent call last):
  File "t.py", line 13, in <module>
    mock()
  File "t.py", line 4, in mock
    lumberjack()
  File "t.py", line 7, in lumberjack
    bright_side_of_death()
  File "t.py", line 10, in bright_side_of_death
    return tuple()[1]
IndexError: tuple index out of range
----------------------------------------------------------------------------------------------------
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
----------------------------------------------------------------------------------------------------
*** format_exception:
['Traceback (most recent call last):\n', '  File "t.py", line 13, in <module>\n    mock()\n', '  File "t.py", line 4, in mock\n    lumberjack()\n', '  File "t.py", line 7, in lumberjack\n    bright_side_of_death()\n', '  File "t.py", line 10, in bright_side_of_death\n    return tuple()[1]\n', 'IndexError: tuple index out of range\n']
----------------------------------------------------------------------------------------------------
*** extract_tb:
[('t.py', 13, '<module>', 'mock()'), ('t.py', 4, 'mock', 'lumberjack()'), ('t.py', 7, 'lumberjack', 'bright_side_of_death()'), ('t.py', 10, 'bright_side_of_death', 'return tuple()[1]')]
----------------------------------------------------------------------------------------------------
*** extract_stack:
[('t.py', 47, '<module>', 'print traceback.extract_stack()')]
----------------------------------------------------------------------------------------------------
*** format_tb:
['  File "t.py", line 13, in <module>\n    mock()\n', '  File "t.py", line 4, in mock\n    lumberjack()\n', '  File "t.py", line 7, in lumberjack\n    bright_side_of_death()\n', '  File "t.py", line 10, in bright_side_of_death\n    return tuple()[1]\n']
----------------------------------------------------------------------------------------------------
*** tb_lineno: 13
['  File "spam.py", line 3, in <module>\n    spam.eggs()\n', '  File "eggs.py", line 42, in eggs\n    return "bacon"\n']

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值