Python中獲取異常(Exception)信息


  異常信息的獲取對於程序的調試非常重要,可以有助於快速定位有錯誤程序語句的位置。下面介紹幾種python中獲取異常信息的方法,這里獲取異常(Exception)信息采用try...except...程序結構。如下所示

try:

  ...

except Exception, e:

  ...

 

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

 

示例如下:

import traceback

print '########################################################'
print "1/0 Exception Info"
print '---------------------------------------------------------'
try:
    1/0
except Exception, e:
    print 'str(Exception):\t', str(Exception)
    print 'str(e):\t\t', str(e)
    print 'repr(e):\t', repr(e)
    print 'e.message:\t', e.message
    print 'traceback.print_exc():'; traceback.print_exc()
    print 'traceback.format_exc():\n%s' % traceback.format_exc()
print '########################################################'
print '\n########################################################'  
print "i = int('a') Exception Info"
print '---------------------------------------------------------'
try:
    i = int('a')
except Exception, e:
    print 'str(Exception):\t', str(Exception)
    print 'str(e):\t\t', str(e)
    print 'repr(e):\t', repr(e)
    print 'e.message:\t', e.message
    print 'traceback.print_exc():'; traceback.print_exc()
    print 'traceback.format_exc():\n%s' % traceback.format_exc()
print '########################################################' 

 

示例結果

 

補充 1(更新於2020.8.1)

對於 Python 3 的 Exception,與 Python 2 的 Exception 相比,有兩個需要注意的地方:

1)在 Python 3 Exception 的 except 子句中,不支持使用逗號 ',' 分隔 Exception 和 e,所以需要采用 as 關鍵詞進行替換;

2)與 Python 2 Exception 類相比,Python 3 Exception 類沒有 message 成員變量。針對這個問題,可以采用 sys.exc_info() 方法獲取得到相關的異常信息。以 1/0 異常處理為例,更新的程序如下:

import sys import traceback print('########################################################') print("1/0 Exception Info") print('---------------------------------------------------------') try: 1/0 except Exception as e: print('str(Exception):\t', str(Exception)) print('str(e):\t\t', str(e)) print('repr(e):\t', repr(e)) # Get information about the exception that is currently being handled 
    exc_type, exc_value, exc_traceback = sys.exc_info() print('e.message:\t', exc_value) print("Note, object e and exc of Class %s is %s the same." % (type(exc_value), ('not', '')[exc_value is e])) print('traceback.print_exc(): ', traceback.print_exc()) print('traceback.format_exc():\n%s' % traceback.format_exc())
print('########################################################')

注:

1) sys.exc_info() 方法可以獲取正在處理的異常信息,即 except 子句正在處理的異常,其返回值為一個tuple類型的三元組(exc_type, exc_value, exc_traceback),其中,exc_type為獲取到的異常類型;exc_value為該異常類型對象;exc_traceback為一個 traceback 對象,包含異常最初發生的調用棧信息。

2) as 關鍵字以及 sys.exc_info() 方法對於 Python 2 同樣適用。

3) 程序中的變量 e 和 exc_value 是同一個異常類型實例對象。

 

參考資料:

Getting the exception value in Python


免責聲明!

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



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