天天看點

python異常資訊擷取

1、python調試的時候擷取異常資訊

1 import traceback
 2 
 3 print '########################################################'
 4 print "1/0 Exception Info"
 5 print '---------------------------------------------------------'
 6 try:
 7     1/0
 8 except Exception, e:
 9     print 'str(Exception):\t', str(Exception)
10     print 'str(e):\t\t', str(e)
11     print 'repr(e):\t', repr(e)
12     print 'e.message:\t', e.message
13     print 'traceback.print_exc():'; traceback.print_exc()
14     print 'traceback.format_exc():\n%s' % traceback.format_exc()
15 print '########################################################'
16 print '\n########################################################'  
17 print "i = int('a') Exception Info"
18 print '---------------------------------------------------------'
19 try:
20     i = int('a')
21 except Exception, e:
22     print 'str(Exception):\t', str(Exception)
23     print 'str(e):\t\t', str(e)
24     print 'repr(e):\t', repr(e)
25     print 'e.message:\t', e.message
26     print 'traceback.print_exc():'; traceback.print_exc()
27     print 'traceback.format_exc():\n%s' % traceback.format_exc()
28 print '########################################################'      

異常資訊:

1 ########################################################
 2 1/0 Exception Info
 3 ---------------------------------------------------------
 4 str(Exception):    <type 'exceptions.Exception'>
 5 str(e):        integer division or modulo by zero
 6 repr(e):    ZeroDivisionError('integer division or modulo by zero',)
 7 e.message:    integer division or modulo by zero
 8 traceback.print_exc():
 9 Traceback (most recent call last):
10   File "tmp.py", line 7, in <module>
11     1/0
12 ZeroDivisionError: integer division or modulo by zero
13 traceback.format_exc():
14 Traceback (most recent call last):
15   File "tmp.py", line 7, in <module>
16     1/0
17 ZeroDivisionError: integer division or modulo by zero
18 
19 ########################################################
20 
21 ########################################################
22 i = int('a') Exception Info
23 ---------------------------------------------------------
24 str(Exception):    <type 'exceptions.Exception'>
25 str(e):        invalid literal for int() with base 10: 'a'
26 repr(e):    ValueError("invalid literal for int() with base 10: 'a'",)
27 e.message:    invalid literal for int() with base 10: 'a'
28 traceback.print_exc():
29 Traceback (most recent call last):
30   File "tmp.py", line 20, in <module>
31     i = int('a')
32 ValueError: invalid literal for int() with base 10: 'a'
33 traceback.format_exc():
34 Traceback (most recent call last):
35   File "tmp.py", line 20, in <module>
36     i = int('a')
37 ValueError: invalid literal for int() with base 10: 'a'
38 
39 ########################################################      

3、說明:

1、str(e)

傳回字元串類型,隻給出異常資訊,不包括異常資訊的類型,如1/0的異常資訊

'integer division or modulo by zero'

2、repr(e)

給出較全的異常資訊,包括異常資訊的類型,如1/0的異常資訊

"ZeroDivisionError('integer division or modulo by zero',)"

3、e.message

獲得的資訊同str(e)

4、采用traceback子產品

  需要導入traceback子產品,此時擷取的資訊最全,與python指令行運作程式出現錯誤資訊一緻。使用traceback.print_exc()列印異常資訊到标準錯誤,就像沒有擷取一樣,或者使用traceback.format_exc()将同樣的輸出擷取為字元串。你可以向這些函數傳遞各種各樣的參數來限制輸出,或者重新列印到像檔案類型的對象。