2. about me
● web backend developer
● interest in:
○ operator system
○ programming language
● http://about.me/ya790206
3. 先看個例外
Traceback (most recent call last):
File "b.py", line 14, in <module>
print func(lst, a)
File "b.py", line 8, in func
raise MyIndexException("list index out of range")
__main__.MyIndexException: list index out of range
4. 那個例外所對應的程式碼
import pickle
class Exception(Exception):
pass
def func(lst, index):
if index < len(lst):
return lst[index]
raise MyIndexException("list index out of range")
a = input()
with open('data.txt', 'r') as ftr:
lst = pickle.load(ftr)
print func(lst, a)
5. 例外告訴我們什麼?
1. 在 b.py 第 8 行(在 func)出錯。原因是 list
index out of range。
2. 在 b.py 第 14 行(在 func)呼叫
3. 例外的類型是 __main__.MyIndexException
7. 方法一
import pickle
class Exception(Exception):
pass
def func(lst, index):
if index < len(lst):
return lst[index]
raise MyIndexException("list index out of range" +str(locals()))
a = input()
with open('data.txt', 'r') as ftr:
lst = pickle.load(ftr)
print func(lst, a)
8. 真的吐出區域變數的資訊了
Traceback (most recent call last):
File "c.py", line 14, in <module>
print func(lst, a)
File "c.py", line 8, in func
raise MyIndexException("list index out of range. " + str
(locals()))
__main__.MyIndexException: list index out of range.
{'index': 8, 'lst': [1, 2, 3]}
11. 效果等價方法一,更好用了
Traceback (most recent call last):
File "c.py", line 14, in <module>
print func(lst, a)
File "c.py", line 8, in func
raise MyIndexException("list index out of range. " + str
(locals()))
__main__.MyIndexException: list index out of range.
{'index': 8, 'lst': [1, 2, 3]}
13. 如果例外是內建的例外 ...
import pickle
def func(lst, index):
return lst[index]
a = input()
with open('data.txt', 'r') as ftr:
lst = pickle.load(ftr)
print func(lst, a)
14. 如果例外是內建的例外 ...
Traceback (most recent call last):
File "a.py", line 11, in <module>
print func(lst, a)
File "a.py", line 5, in func
return lst[index]
IndexError: list index out of range
15. How to solve it?
● 如果你想修改 IndexError 的建構子的話
● 你需要修改 python source code,並自己編譯
他…
● It’s hard for me.
20. 設計緣由
為什麼我會希望把錯誤當時的 run time 資訊顯
示出來? bug 可以分成三種:一種是程式錯誤,
一種是資料錯誤,一種是前兩者都是錯的。過去
維護的一個系統,裡面資料很多都是錯的。因此
每當客戶回報系統有問題時,需要花時間去釐清
哪種錯誤。如果能夠取得錯誤時的 run time 資
訊,可以加速重建錯誤的情形,以利除錯。