国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python traceback模塊

2020-01-04 17:37:18
字體:
來源:轉載
供稿:網友
traceback模塊被用來跟蹤異常返回信息. 如下例所示:
  1. import traceback
  2. try:
  3.     raise SyntaxError, "traceback test"
  4. except:
  5.     traceback.print_exc()
將會在控制臺輸出類似結果:
  1. Traceback (most recent call last):
  2.  
  3. File "H:/PythonWorkSpace/Test/src/TracebackTest.py", line 3, in <module>
  4.  
  5. raise SyntaxError, "traceback test"
  6.  
  7. SyntaxError: traceback test

類似在你沒有捕獲異常時候, 解釋器所返回的結果.
你也可以傳入一個文件, 把返回信息寫到文件中去, 如下:
  1. import traceback
  2. import StringIO
  3. try:
  4.     raise SyntaxError, "traceback test"
  5. except:
  6.     fp = StringIO.StringIO() #創建內存文件對象
  7.     traceback.print_exc(file=fp)
  8.     message = fp.getvalue()
  9.     print message
這樣在控制臺輸出的結果和上面例子一樣
traceback模塊還提供了extract_tb函數來格式化跟蹤返回信息, 得到包含錯誤信息的列表, 如下:
  1. import traceback
  2. import sys
  3.  
  4. def tracebacktest():
  5.     raise SyntaxError, "traceback test"
  6. try:
  7.     tracebacktest()
  8. except:
  9.     info = sys.exc_info()
  10.     for file, lineno, function, text in traceback.extract_tb(info[2]):
  11.         print file, "line:", lineno, "in", function
  12.         print text
  13.     print "** %s: %s" % info[:2]
控制臺輸出結果如下:
  1. H:/PythonWorkSpace/Test/src/TracebackTest.py line: 7 in <module>
  2.  
  3. tracebacktest()
  4.  
  5. H:/PythonWorkSpace/Test/src/TracebackTest.py line: 5 in tracebacktest
  6.  
  7. raise SyntaxError, "traceback test"
  8.  
  9. ** <type 'exceptions.SyntaxError'>: traceback test

Example 2-18 展示了 traceback 模塊允許你在程序里打印異常的跟蹤返回 (Traceback)信息, 類似未捕獲異常時解釋器所做的. 如 Example 2-18 所示. 2.11.0.1. Example 2-18. 使用 traceback 模塊打印跟蹤返回信息 File: traceback-example-1.py # note! import
Example 2-18 展示了 traceback 模塊允許你在程序里打印異常的跟蹤返回 (Traceback)信息, 類似未捕獲異常時解釋器所做的. 如 Example 2-18 所示.

2.11.0.1. Example 2-18. 使用 traceback 模塊打印跟蹤返回信息

  1. File: traceback-example-1.py
  2.  
  3. # importing the traceback module messes up the
  4. # exception state, so you better do that here and not
  5. # in the exception handler
  6. # 導入 traceback 會清理掉異常狀態, 所以
  7. # 最好別在異常處理代碼中導入該模塊
  8. import traceback
  9.  
  10. try:
  11.     raise SyntaxError, "example"
  12. except:
  13.     traceback.print_exc()
  1. Traceback (innermost last):
  2.  
  3. File "traceback-example-1.py", line 7, in ?
  4.  
  5. SyntaxError: example

Example 2-19 使用 StringIO 模塊將跟蹤返回信息放在字符串中.

2.11.0.2. Example 2-19. 使用 traceback 模塊將跟蹤返回信息復制到字符串
File: traceback-example-2.py

  1. import traceback
  2. import StringIO
  3.  
  4. try:
  5.     raise IOError, "an i/o error occurred"
  6. except:
  7.     fp = StringIO.StringIO()
  8.     traceback.print_exc(file=fp)
  9.     message = fp.getvalue()
  10.  
  11.     print "failure! the error was:", repr(message)
  1. failure! the error was: 'Traceback (innermost last):/012 File
  2.  
  3. "traceback-example-2.py", line 5, in ?/012IOError: an i/o error
  4.  
  5. occurred/012'

你可以使用 extract_tb 函數格式化跟蹤返回信息, 得到包含錯誤信息的列表, 如 Example 2-20 所示.

2.11.0.3. Example 2-20. 使用 traceback Module 模塊編碼 Traceback 對象
File: traceback-example-3.py

  1. import traceback
  2. import sys
  3.  
  4. def function():
  5.     raise IOError, "an i/o error occurred"
  6.  
  7. try:
  8.     function()
  9. except:
  10.     info = sys.exc_info()
  11.     for file, lineno, function, text in traceback.extract_tb(info[2]):
  12.         print file, "line", lineno, "in", function
  13.         print "=>", repr(text)
  14.     print "** %s: %s" % info[:2]
  1. traceback-example-3.py line 8 in ?
  2.  
  3. => 'function()'
  4.  
  5. traceback-example-3.py line 5 in function
  6.  
  7. => 'raise IOError, "an i/o error occurred"'
  8.  
  9. ** exceptions.IOError: an i/o error occurred

文章來自 http://docs.python.org/dev/library/traceback.html

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴青县| 隆安县| 西乌珠穆沁旗| 巴塘县| 长阳| 葫芦岛市| 丹棱县| 泗洪县| 福州市| 临高县| 班戈县| 莱西市| 白河县| 武宣县| 卢龙县| 新宁县| 右玉县| 莆田市| 甘谷县| 乐业县| 体育| 建宁县| 阳信县| 辰溪县| 桃园市| 大丰市| 桂东县| 永嘉县| 论坛| 上思县| 怀来县| 千阳县| 卫辉市| 沈阳市| 岑溪市| 泾阳县| 深圳市| 小金县| 嵩明县| 普安县| 徐州市|