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()將同樣的輸出獲取為字符串。你可以向這些函數傳遞各種各樣的參數來限制輸出,或者重新打印到像文件類型的對象。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM