SlideShare a Scribd company logo
1 of 20
Download to read offline
那些年,我們一起看
的例外
果凍
about me
● web backend developer
● interest in:
○ operator system
○ programming language

● http://about.me/ya790206
先看個例外
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
那個例外所對應的程式碼
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)
例外告訴我們什麼?
1. 在 b.py 第 8 行(在 func)出錯。原因是 list
index out of range。
2. 在 b.py 第 14 行(在 func)呼叫
3. 例外的類型是 __main__.MyIndexException
心中疑惑
● lst 元素到底有幾個,還是根本沒有元素?
● a 那個數字到底多大?
方法一
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)
真的吐出區域變數的資訊了
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]}
● 這程式碼看起來很有點蠢
● 有沒有更好的辦法?
在例外建構子做一些手腳,方法二
import pickle
import inspect
class MyIndexException(Exception):
def __init__(self, msg):
msg += str(inspect.stack()[1][0].f_locals)
super(MyIndexException, self).__init__(msg)
下略 ...
效果等價方法一,更好用了
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]}
● 這一切看起來很美好。也只有看起來。
● 如果 ... 程式碼變這樣?
如果例外是內建的例外 ...
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)
如果例外是內建的例外 ...
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
How to solve it?
● 如果你想修改 IndexError 的建構子的話
● 你需要修改 python source code,並自己編譯
他…
● It’s hard for me.
其他解,方法三
● 利用 trace function 追蹤
○ https://github.
com/ya790206/log_exception/blob/master
/log_exception.py
○ 無法和 pdb 共存
○ 會降低 python 效能,因此不適用於
production。
其他可能解
● monkey patch 所有內建例外?
總結
● 方法一:程式碼有點醜陋。每次都要傳參數進
去有點麻煩。
● 方法二:只有當錯誤發生時,才自動做紀錄。可
避免方法三的效能問題。但要注意上一個的
call stack 未必是你要的資料。如子類別
override 建構子時。又此法只能用在自訂類
別。
總結
● 方法三:可以印出所有例外發生時的 call stack
資訊。缺點為嚴重影響效能。只適合用在開發
時期。
設計緣由
為什麼我會希望把錯誤當時的 run time 資訊顯
示出來? bug 可以分成三種:一種是程式錯誤,
一種是資料錯誤,一種是前兩者都是錯的。過去
維護的一個系統,裡面資料很多都是錯的。因此
每當客戶回報系統有問題時,需要花時間去釐清
哪種錯誤。如果能夠取得錯誤時的 run time 資
訊,可以加速重建錯誤的情形,以利除錯。

More Related Content

What's hot

《Java程序设计》期末考试试题 (六)
《Java程序设计》期末考试试题 (六)《Java程序设计》期末考试试题 (六)
《Java程序设计》期末考试试题 (六)jane2006
 
PHPUnit + Xdebug 单元测试技术
PHPUnit + Xdebug 单元测试技术PHPUnit + Xdebug 单元测试技术
PHPUnit + Xdebug 单元测试技术hoopchina
 
Free Marker中文文档
Free Marker中文文档Free Marker中文文档
Free Marker中文文档yiditushe
 
10 檔案說明與處理
10 檔案說明與處理10 檔案說明與處理
10 檔案說明與處理shademoon
 
C語言 第4章 基本輸出與輸入功能
C語言 第4章 基本輸出與輸入功能C語言 第4章 基本輸出與輸入功能
C語言 第4章 基本輸出與輸入功能shademoon
 
Introduction to Basic Haskell Components (In Chinese)
Introduction to Basic Haskell Components (In Chinese)Introduction to Basic Haskell Components (In Chinese)
Introduction to Basic Haskell Components (In Chinese)ChengHui Weng
 
.Net 技術研討(linq與架構開發)
.Net 技術研討(linq與架構開發).Net 技術研討(linq與架構開發)
.Net 技術研討(linq與架構開發)Gelis Wu
 

What's hot (20)

Python串列資料應用
Python串列資料應用Python串列資料應用
Python串列資料應用
 
Python程式設計 - 串列資料應用
Python程式設計 - 串列資料應用 Python程式設計 - 串列資料應用
Python程式設計 - 串列資料應用
 
《Java程序设计》期末考试试题 (六)
《Java程序设计》期末考试试题 (六)《Java程序设计》期末考试试题 (六)
《Java程序设计》期末考试试题 (六)
 
Python程式設計 - 迴圈作業
Python程式設計 - 迴圈作業Python程式設計 - 迴圈作業
Python程式設計 - 迴圈作業
 
PHPUnit + Xdebug 单元测试技术
PHPUnit + Xdebug 单元测试技术PHPUnit + Xdebug 单元测试技术
PHPUnit + Xdebug 单元测试技术
 
Free Marker中文文档
Free Marker中文文档Free Marker中文文档
Free Marker中文文档
 
Ch12 教學
Ch12 教學Ch12 教學
Ch12 教學
 
10 檔案說明與處理
10 檔案說明與處理10 檔案說明與處理
10 檔案說明與處理
 
Appendix B 教學
Appendix B 教學Appendix B 教學
Appendix B 教學
 
Ppt 120-126
Ppt 120-126Ppt 120-126
Ppt 120-126
 
Python 迴圈作業
Python 迴圈作業Python 迴圈作業
Python 迴圈作業
 
Ch7
Ch7Ch7
Ch7
 
C語言結構與串列
C語言結構與串列 C語言結構與串列
C語言結構與串列
 
Ch9 教學
Ch9 教學Ch9 教學
Ch9 教學
 
Python基本資料運算
Python基本資料運算Python基本資料運算
Python基本資料運算
 
Ch10
Ch10Ch10
Ch10
 
C語言 第4章 基本輸出與輸入功能
C語言 第4章 基本輸出與輸入功能C語言 第4章 基本輸出與輸入功能
C語言 第4章 基本輸出與輸入功能
 
Introduction to Basic Haskell Components (In Chinese)
Introduction to Basic Haskell Components (In Chinese)Introduction to Basic Haskell Components (In Chinese)
Introduction to Basic Haskell Components (In Chinese)
 
.Net 技術研討(linq與架構開發)
.Net 技術研討(linq與架構開發).Net 技術研討(linq與架構開發)
.Net 技術研討(linq與架構開發)
 
Ppt 136-140
Ppt 136-140Ppt 136-140
Ppt 136-140
 

Similar to 那些年,我們一起看的例外

Python学习笔记
Python学习笔记Python学习笔记
Python学习笔记Lingfei Kong
 
Baidu LSP and DISQL for Log Analysis
Baidu LSP and DISQL for Log AnalysisBaidu LSP and DISQL for Log Analysis
Baidu LSP and DISQL for Log AnalysisXiaoming Chen
 
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫Justin Lin
 
HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享Chong-Kuan Chen
 
Node.js开发体验
Node.js开发体验Node.js开发体验
Node.js开发体验QLeelulu
 
ncuma_函數微分計算.pptx
ncuma_函數微分計算.pptxncuma_函數微分計算.pptx
ncuma_函數微分計算.pptxNCU MCL
 
函數微分_範例.pptx
函數微分_範例.pptx函數微分_範例.pptx
函數微分_範例.pptxmclmath
 
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
lambda/closure – JavaScript、Python、Scala 到 Java SE 7lambda/closure – JavaScript、Python、Scala 到 Java SE 7
lambda/closure – JavaScript、Python、Scala 到 Java SE 7Justin Lin
 
第01章 绪论(java版)
第01章  绪论(java版)第01章  绪论(java版)
第01章 绪论(java版)Yan Li
 
Sql培训 (1)
Sql培训 (1)Sql培训 (1)
Sql培训 (1)jhao niu
 
Ecma script edition5-小试
Ecma script edition5-小试Ecma script edition5-小试
Ecma script edition5-小试lydiafly
 
PHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming SkillsPHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming SkillsHo Kim
 
系統程式 -- 第 12 章
系統程式 -- 第 12 章系統程式 -- 第 12 章
系統程式 -- 第 12 章鍾誠 陳鍾誠
 
twMVC#27 | C# 7.0 新功能介紹
twMVC#27 | C# 7.0 新功能介紹twMVC#27 | C# 7.0 新功能介紹
twMVC#27 | C# 7.0 新功能介紹twMVC
 
基于Eclipse和hadoop平台应用开发入门手册
基于Eclipse和hadoop平台应用开发入门手册基于Eclipse和hadoop平台应用开发入门手册
基于Eclipse和hadoop平台应用开发入门手册Zhen Li
 
Python速成指南
Python速成指南Python速成指南
Python速成指南March Liu
 
C++工程实践
C++工程实践C++工程实践
C++工程实践Shuo Chen
 

Similar to 那些年,我們一起看的例外 (20)

Python学习笔记
Python学习笔记Python学习笔记
Python学习笔记
 
Baidu LSP and DISQL for Log Analysis
Baidu LSP and DISQL for Log AnalysisBaidu LSP and DISQL for Log Analysis
Baidu LSP and DISQL for Log Analysis
 
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
 
HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享
 
Node.js开发体验
Node.js开发体验Node.js开发体验
Node.js开发体验
 
ncuma_函數微分計算.pptx
ncuma_函數微分計算.pptxncuma_函數微分計算.pptx
ncuma_函數微分計算.pptx
 
函數微分_範例.pptx
函數微分_範例.pptx函數微分_範例.pptx
函數微分_範例.pptx
 
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
lambda/closure – JavaScript、Python、Scala 到 Java SE 7lambda/closure – JavaScript、Python、Scala 到 Java SE 7
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
 
第01章 绪论(java版)
第01章  绪论(java版)第01章  绪论(java版)
第01章 绪论(java版)
 
functional-scala
functional-scalafunctional-scala
functional-scala
 
Sql培训 (1)
Sql培训 (1)Sql培训 (1)
Sql培训 (1)
 
Ecma script edition5-小试
Ecma script edition5-小试Ecma script edition5-小试
Ecma script edition5-小试
 
PHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming SkillsPHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming Skills
 
Sym py edu
Sym py eduSym py edu
Sym py edu
 
系統程式 -- 第 12 章
系統程式 -- 第 12 章系統程式 -- 第 12 章
系統程式 -- 第 12 章
 
twMVC#27 | C# 7.0 新功能介紹
twMVC#27 | C# 7.0 新功能介紹twMVC#27 | C# 7.0 新功能介紹
twMVC#27 | C# 7.0 新功能介紹
 
Hi Haskell
Hi HaskellHi Haskell
Hi Haskell
 
基于Eclipse和hadoop平台应用开发入门手册
基于Eclipse和hadoop平台应用开发入门手册基于Eclipse和hadoop平台应用开发入门手册
基于Eclipse和hadoop平台应用开发入门手册
 
Python速成指南
Python速成指南Python速成指南
Python速成指南
 
C++工程实践
C++工程实践C++工程实践
C++工程实践
 

More from kao kuo-tung

用 Open source 改造鍵盤
用 Open source 改造鍵盤用 Open source 改造鍵盤
用 Open source 改造鍵盤kao kuo-tung
 
Immutable infrastructure 介紹與實做:以 kolla 為例
Immutable infrastructure 介紹與實做:以 kolla 為例Immutable infrastructure 介紹與實做:以 kolla 為例
Immutable infrastructure 介紹與實做:以 kolla 為例kao kuo-tung
 
Openstack swift, how does it work?
Openstack swift, how does it work?Openstack swift, how does it work?
Openstack swift, how does it work?kao kuo-tung
 
Why is a[1] fast than a.get(1)
Why is a[1]  fast than a.get(1)Why is a[1]  fast than a.get(1)
Why is a[1] fast than a.get(1)kao kuo-tung
 
減少重複的測試程式碼的一些方法
減少重複的測試程式碼的一些方法減少重複的測試程式碼的一些方法
減少重複的測試程式碼的一些方法kao kuo-tung
 
Openstack taskflow 簡介
Openstack taskflow 簡介Openstack taskflow 簡介
Openstack taskflow 簡介kao kuo-tung
 
Async: ways to store state
Async:  ways to store stateAsync:  ways to store state
Async: ways to store statekao kuo-tung
 
Docker 原理與實作
Docker 原理與實作Docker 原理與實作
Docker 原理與實作kao kuo-tung
 
Python 中 += 與 join比較
Python 中 += 與 join比較Python 中 += 與 join比較
Python 中 += 與 join比較kao kuo-tung
 
Garbage collection 介紹
Garbage collection 介紹Garbage collection 介紹
Garbage collection 介紹kao kuo-tung
 
Python 如何執行
Python 如何執行Python 如何執行
Python 如何執行kao kuo-tung
 
recover_pdb 原理與介紹
recover_pdb 原理與介紹recover_pdb 原理與介紹
recover_pdb 原理與介紹kao kuo-tung
 

More from kao kuo-tung (15)

用 Open source 改造鍵盤
用 Open source 改造鍵盤用 Open source 改造鍵盤
用 Open source 改造鍵盤
 
Immutable infrastructure 介紹與實做:以 kolla 為例
Immutable infrastructure 介紹與實做:以 kolla 為例Immutable infrastructure 介紹與實做:以 kolla 為例
Immutable infrastructure 介紹與實做:以 kolla 為例
 
Python to scala
Python to scalaPython to scala
Python to scala
 
Intorduce to Ceph
Intorduce to CephIntorduce to Ceph
Intorduce to Ceph
 
Openstack swift, how does it work?
Openstack swift, how does it work?Openstack swift, how does it work?
Openstack swift, how does it work?
 
Why is a[1] fast than a.get(1)
Why is a[1]  fast than a.get(1)Why is a[1]  fast than a.get(1)
Why is a[1] fast than a.get(1)
 
減少重複的測試程式碼的一些方法
減少重複的測試程式碼的一些方法減少重複的測試程式碼的一些方法
減少重複的測試程式碼的一些方法
 
Openstack taskflow 簡介
Openstack taskflow 簡介Openstack taskflow 簡介
Openstack taskflow 簡介
 
Async: ways to store state
Async:  ways to store stateAsync:  ways to store state
Async: ways to store state
 
Openstack 簡介
Openstack 簡介Openstack 簡介
Openstack 簡介
 
Docker 原理與實作
Docker 原理與實作Docker 原理與實作
Docker 原理與實作
 
Python 中 += 與 join比較
Python 中 += 與 join比較Python 中 += 與 join比較
Python 中 += 與 join比較
 
Garbage collection 介紹
Garbage collection 介紹Garbage collection 介紹
Garbage collection 介紹
 
Python 如何執行
Python 如何執行Python 如何執行
Python 如何執行
 
recover_pdb 原理與介紹
recover_pdb 原理與介紹recover_pdb 原理與介紹
recover_pdb 原理與介紹
 

那些年,我們一起看的例外