Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
Teddy Chen
Sept. 14 2013
例外處理設計與重構實作班
Copyright@2013 Teddysoft
我是誰
• 2012年7月成立泰迪軟體,從事敏捷開發顧問、教
育訓練、軟體工具導入等服務。
• 2012年6月,出版暢銷書「笑談軟體工程:敏捷方
法的逆襲」。
• 2012年4月起,多次講授Scrum課程,與學員互動氣
氛佳,滿意度高。
• 超...
課程內容
• 例外處理基本觀念
• 例外處理的4+1觀點
• 建立例外處理中心思想—Staged Robustness
Model
• EH Bad Smells and Refactoring's
Copyright@2013 Teddyso...
例外處理基本觀念
一個軟體開發專案存在著很多互相競爭
且衝突的非功能需求
Copyright@2013 Teddysoft
robustness
(exception handling)
time-to-market, iterative &
increment...
Robustness輸了之後會造成系統不穩定
Copyright@2013 Teddysoft
系統不穩定會有什麼問題?
Copyright@2013 Teddysoft
輕則損失時間、金錢與商譽,
重則可能危害生命安全。
SPECIFICATION
Correctness
Robustness
提升軟體可靠度需同時考慮
Correctness與Robustness這兩個因素
• Correctness
– 軟體產品可以執行規格中所規範的工作或行為
– 可透過Contract Specification來達成
• Robustness
...
本課程介紹如何透過例外處理
來增加系統的強健度
練習:請分享一個因為例外
處理不良而造成金錢上、時
間上、精神上損失的經驗
例外處理機制—Exception
Handling Mechanism (EHM)
問題: 請說出一個你熟知的程
式語言的例外處理機制
例外處理機制(EHM)是程式語言用來
支援例外處理的方法
• Representation
• Definition
• Signaling
• Propagation
• Resolution
• Continuation
Copyright...
1. Representation
• 程式語言表達例外的方法
– Symbol
• strings or numbers
– Data object
• Used to hold error and failure information o...
2. Definition
• 程式設計師如何定義一個例外
–Symbols
• new exceptions are defined as strings or
numbers.
–Data objects and full objects
...
3. Signaling
• 產生一個例外(的實例),並且將例外傳給
接收者的指令稱之為:
– throwing, signaling, raising, or triggering
• 例外產生方式有兩種:
– Synchronous exc...
4. Propagation
• If an exception is signaled and not coped
with locally, the exception can be propagated
to the caller of ...
5. Resolution
• Exception resolution or handler binding is a process
of finding a suitable handler in the target, which is...
6. Continuation
• An exception continuation or exception
model specifies the execution flow after
an exception handler ret...
容易搞混且重要的觀念:Fault, Error,
Failure, Exception彼此的關係
fault error failure
exception
(1) design
(2) component
cause of failure a...
以下何者是design fault,何者是component
fault?
1. 除以零 (division by zero)
2. Index Out of Bound
3. 網路斷線
4. 硬碟空間已滿
5. 檔案不存在
24
Copyri...
為什麼要區分design fault與
component fault?
Exception Handling vs. Fault-Tolerant
Programming
• Exception handling deals with component faults
(anticipated exceptions...
Java例外處理機制
Java Exception Handling: The try
Statement (before JDK 7)
28Copyright@2013 Teddysoft
Java Exception Handling: The
try_multi_catch in JDK 7
29Copyright@2013 Teddysoft
Java Exception Handling: The
try_with_resources in JDK 7
30Copyright@2013 Teddysoft
Java Exception Class Hierarchy
31
checked
unchecked
IOException
NullPointerException
Throwable
Exception
RuntimeException
...
Use checked exceptions for
recoverable conditions and
run-time exceptions for
programming errors
使用Checked Exception須遵循Handle-or-Declare Rule
Copyright@2013 Teddysoft
handle
declare
程式如果違反Handle-or-
Declare Rule將被Java Compiler
視為語法錯誤
例外處理的4+1觀點
Usage (用途)
Design (設計)
Handling (處理)
Tool-Support (工具支援)
Process (流程)
為什麼例外處理這麼難?
Usage View (例外用途觀點)
Exception, 真的只是用來代表
「例外狀況」嗎?
練習:分組討論要如何處理
EOFException與
InterruptedException?
EOFException範例
public void readDataFromFile(String aFileName){
try (DataInputStream input = new DataInputStream
(new FileI...
InterruptedException範例
public void sleepMillisecond(int ms){
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
//...
Usage View
• Failure
• Notification
– EOFException
– InterruptedException
Copyright@2013 Teddysoft
案情沒有那麼單純,請看另一個
EOFException範例
Copyright@2013 Teddysoft
public void fetchRawBytesAndSetupMessage(DataInputStream aIS)
throw...
Context 決定 exception的用途
Design View
(例外設計觀點)
Design View
• Declared:
– 例外有宣告在元件的介面規範中
– 又稱為anticipated或expected例外
– 代表component fault
• Undeclared:
– 例外沒有宣告在元件的介面規範中
–...
Declared Exception
Copyright@2013 Teddysoft
public void fetchRawBytesAndSetupMessage(DataInputStream aIS)
throws IOExcepti...
Undeclared Exception (1/2)
Copyright@2013 Teddysoft
public Hamburg createHamburger(String type) {
Hamburg ham = null;
swit...
Undeclared Exception (2/2)
Copyright@2013 Teddysoft
public void deposit(int value) {
if (value < 0 ) {
throw new IllegalAr...
你在做例外處理還是容錯設計? (1/2)
Copyright@2013 Teddysoft
public void deposit(int value) throws
llegalArgumentException {
if (value < ...
你在做例外處理還是容錯設計? (2/2)
public String execute(String cmd) throws
IOException,
NullPointerException,
IllegalArgumentException;...
Design View小結
撇開程式語言是否區分checked與
unchecked例外,唯有將例外宣告在介面
上(或以某種形式存在程式或文件中),
在設計階段程式設計師才有機會知道要
如何來因應可能會遭遇到的異常狀況。
Copyright@2...
Handling View
(例外處理觀點)
Handling View
• Recoverability (可恢復性)
– recoverable, unrecoverable(irrecoverable)
• Exception handling constructs in
langu...
Handling View之
Recoverability
Recoverability:爽到你,艱苦到我
Copyright@2013 Teddysoft
Thanks Linda
Recoverability:Callee與Caller都要負責任
Copyright@2013 Teddysoft
Thanks Linda
public void sleepMillisecond(int ms){
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
// How to "handle" this e...
public void readDataFromFile(String aFileName){
try (DataInputStream input = new DataInputStream
(new FileInputStream(aFil...
public void fetchRawBytesAndSetupMessage(DataInputStream aIS)
throws IOException, InvalidPacketException {
int length = aI...
Handling View之
Exception Handling Constructs
and Utilities
不同的程式語言有不同的例外處理構件
• Java/C#
– try-catch-finally
• C++
– try-catch
– destructor
• Eiffel
– Exception handlers in Eiffel are...
重新思考try-catch-finally的責任與分工
• Try
– Implement requirements (can have alternatives)
– Prepare state recovery (e.g., make a ...
例外處理也是一種程式設計,需要程式
設計能力與軟體元件支援
• 設計技巧
– Memento、Smart pointer、Check point、etc.
– Exception hierarchy
– EH best practices an...
Handling View小結
要判斷一個例外是否為一個可修復的狀
況,是例外處理「設計」的第一個步驟,
但這個判斷依據並不是一件容易的事。
確定了例外的recoverability之後,接著
可利用程式語言構件與軟體元件的協助
來實作例外處理...
Tool-Support View
(例外工具支援觀點)
Tool-Support View
• Java語言的tool-support
– 區分Checked與unchecked例外
Copyright@2013 Teddysoft
Java與C#程式比較
Copyright@2013 Teddysoft
Java語言的Tool-Support所造成的後遺症
• Interface evolution problem
• Ignored checked exception
68
Copyright@2013 Teddysoft
Tool-Support View小結
為了提高軟體的強健度,開發人員
需要一個提醒機制,告知那些操作
有可能產生例外,否則開發人員更
容易忽略例外處理,只能等runtime
發生錯誤時再回頭修補。
Copyright@2013 Teddyso...
Process View
(開發流程觀點)
Process View
• Waterfall VS. IID (iterative and
incremental development)
• 如何在IID流程中規劃例外處理?
– I will handle this exception...
以Scrum為例
• Story
– Normal scenarios
– Failure scenarios
• 這個sprint先做normal scenarios,下個
sprint再做failure scenarios
Copyrigh...
「先做normal scenarios,再做failure
scenarios」實務上有何問題?
Copyright@2013 Teddysoft
做normal scenarios的時候遇到例外
怎麼辦?
Copyright@2013 Teddysoft
public void fetchRawBytesAndSetupMessage(DataInputStream aIS)
throw...
Process View小結
IID或敏捷開發法不會讓例外處理變
得更簡單。若團隊沒有一套例外處
理設計規範,則很有可能反而會降
低系統的強健度。
Copyright@2013 Teddysoft
例外處理的4+1種觀點結論
例外處理…好難…Orz
建立例外處理中心思想—
Staged Robustness Model
例外處理的目標
例外處理的目標
Robustness Levels (強健度等級)
80
Undefined
Error-
Reporting
State-
Recovery
Behavior-
Recovery
0
1
2
3
Robustness
unpredictabl...
Robustness levels of components
Element RL G0 RL G1 RL G2 RL G3
name undefined error-reporting state-recovery behavior-rec...
Upgrading and degrading
exception handling goals
82
fail-fast; keep
user informed
G0 G1 G2
restore state, clean up,
and ke...
Applicability for the robustness levels
83
RL Applicability
G1
 In the early stage of system development
 Prototyping
 ...
強健度等級小結
Consequences
• Robustness without costly up-front design
• Guiding exception handling implementation
• Independent of lang...
Bad Smells and Refactorings
Refactoring基本觀念
Bad Smells and Refactorings
Refactoring基本觀念
What is Refactoring
• Improving the internal structure of a
software system without altering its
external behavior [fowler...
Refactoring and EH Refactoring
89
Normal
Behavior
Exceptional
Behavior
Refactoring EH Refactoring
Behavior
Copyright@2013 ...
EH Smells, Refactorings, and RL
90
EH smell Refactoring RL
Return code Replace Error Code with Exception G1
Ignored checke...
Smell: Return Code
91
Copyright@2013 Teddysoft
public int withdraw(int amount) {
if (amount > this.balance)
return -1;
els...
Refactoring: Replace Error Code with
Exception
92
Copyright@2013 Teddysoft
public int withdraw(int amount) throws
NotEnoug...
Smell: Ignored Checked Exception
93
Copyright@2013 Teddysoft
public void writeFile(String fileName, String data) {
Writer ...
Replace Ignored Checked Exception
with Unchecked Exception
public void writeFile(String fileName, String data) {
Writer wr...
Smell: Unprotected Main Program
95
Copyright@2013 Teddysoft
static public void main(String[] args) {
MyApp myapp = new MyA...
Avoid Unexpected Termination with
Big Outer Try Block
96
static public void main(String[] args) {
MyApp myapp = new MyApp(...
Smell: Dummy Handler
97
Copyright@2013 Teddysoft
public void m(String aFileName) {
try{
FileInputStream fis = new FileInpu...
Replace Dummy Handler with Rethrow
98
Copyright@2013 Teddysoft
public void m(String aFileName)
{
try{
FileInputStream fis ...
Smell: Nested Try Statement
99
Copyright@2013 Teddysoft
FileInputStream in = null;
try {
in = new FileInputStream(…);
}
fi...
Replace Nested Try Statement with
Method
100
FileInputStream in = null;
try {
in = new FileInputStream(…);
}
finally {
try...
Smell: Careless Cleanup
101
Copyright@2013 Teddysoft
public void cleanup String aFileName) {
try{
FileInputStream fis = ne...
Replace Careless Cleanup with Guaranteed
Cleanup
102
Copyright@2013 Teddysoft
public void cleanup String aFileName) {
try{...
練習:尋找 Smells
練習:EH Refactoring
Advanced Refactoring
Introduce Checkpoint Class
106
public void foo () throws FailureException {
try {
/* code that may change the state of the...
Smell: Spare handler
107
Copyright@2013 Teddysoft
try { /* primary */ }
catch (SomeException e) {
try {/* alternative */}
...
Introduce Resourceful Try Clause
108
try { /* primary */ }
catch (SomeException e) {
try {/* alternative */}
catch(Another...
參考資料
Copyright@2013 Teddysoft
複習
• 例外處理基本觀念
– EHM、fault、error、failure、exception
• 例外處理的4+1觀點
• 建立例外處理中心思想—Staged Robustness
Model
• EH Bad Smells and Re...
泰迪軟體敏捷開發訓練藍圖
Copyright@2013 Teddysoft
謝謝,再見 XD
Vous avez terminé ce document.
Prochain SlideShare
Design Patterns這樣學就會了:入門班 Day1 教材
Suivant
Prochain SlideShare
Design Patterns這樣學就會了:入門班 Day1 教材
Suivant

Partager

[教材] 例外處理設計與重構實作班201309

此為泰迪軟體在2013年9月14日舉辦的例外處理設計與重構班完整教材內容。

[教材] 例外處理設計與重構實作班201309

  1. 1. Teddy Chen Sept. 14 2013 例外處理設計與重構實作班
  2. 2. Copyright@2013 Teddysoft
  3. 3. 我是誰 • 2012年7月成立泰迪軟體,從事敏捷開發顧問、教 育訓練、軟體工具導入等服務。 • 2012年6月,出版暢銷書「笑談軟體工程:敏捷方 法的逆襲」。 • 2012年4月起,多次講授Scrum課程,與學員互動氣 氛佳,滿意度高。 • 超過17年design pattern實務經驗,曾在pattern領域 最著名的PLoP國際研討會發表論文。 – PLoP 2004:A Pattern Language for Personal Authoring in E-Learning. – Asia PLoP 2011:Emerging Patterns of Continuous Integration for Cross-Platform Software Development. • 2009年取得Certified ScrumMaster。 • 2008年4月起迄今,5年以上Scrum業界導入經驗。 • 2008年取得台北科技大學資工博士。 • 2007年起經營「搞笑談軟工」部落格。 Copyright@2013 Teddysoft
  4. 4. 課程內容 • 例外處理基本觀念 • 例外處理的4+1觀點 • 建立例外處理中心思想—Staged Robustness Model • EH Bad Smells and Refactoring's Copyright@2013 Teddysoft
  5. 5. 例外處理基本觀念
  6. 6. 一個軟體開發專案存在著很多互相競爭 且衝突的非功能需求 Copyright@2013 Teddysoft robustness (exception handling) time-to-market, iterative & incremental design, maintainability, etc.誰贏、誰輸?
  7. 7. Robustness輸了之後會造成系統不穩定 Copyright@2013 Teddysoft 系統不穩定會有什麼問題?
  8. 8. Copyright@2013 Teddysoft
  9. 9. 輕則損失時間、金錢與商譽, 重則可能危害生命安全。
  10. 10. SPECIFICATION Correctness Robustness
  11. 11. 提升軟體可靠度需同時考慮 Correctness與Robustness這兩個因素 • Correctness – 軟體產品可以執行規格中所規範的工作或行為 – 可透過Contract Specification來達成 • Robustness – 軟體系統應付異常狀況的能力 – 可透過Exception Handling來達成 Copyright@2013 Teddysoft
  12. 12. 本課程介紹如何透過例外處理 來增加系統的強健度
  13. 13. 練習:請分享一個因為例外 處理不良而造成金錢上、時 間上、精神上損失的經驗
  14. 14. 例外處理機制—Exception Handling Mechanism (EHM)
  15. 15. 問題: 請說出一個你熟知的程 式語言的例外處理機制
  16. 16. 例外處理機制(EHM)是程式語言用來 支援例外處理的方法 • Representation • Definition • Signaling • Propagation • Resolution • Continuation Copyright@2013 Teddysoft
  17. 17. 1. Representation • 程式語言表達例外的方法 – Symbol • strings or numbers – Data object • Used to hold error and failure information only. • Raised by a language keyword. – Full object • Encapsulate signaling, propagation, and continuation behaviors of exceptions in the class definition. Copyright@2013 Teddysoft
  18. 18. 2. Definition • 程式設計師如何定義一個例外 –Symbols • new exceptions are defined as strings or numbers. –Data objects and full objects • a class is used to define an exception. Copyright@2013 Teddysoft
  19. 19. 3. Signaling • 產生一個例外(的實例),並且將例外傳給 接收者的指令稱之為: – throwing, signaling, raising, or triggering • 例外產生方式有兩種: – Synchronous exception • A direct result of performing the instruction. – Asynchronous exception • Produced by the runtime environment upon encountering an internal error or by stopping or suspending a thread. Copyright@2013 Teddysoft
  20. 20. 4. Propagation • If an exception is signaled and not coped with locally, the exception can be propagated to the caller of the signaling method. • Exception propagation can be explicit or implicit (or automatic). – Explicit: a receiver must explicitly re-throw an unhandled received exception for further propagation – Implicit: an unhandled exception is automatically propagated. Copyright@2013 Teddysoft
  21. 21. 5. Resolution • Exception resolution or handler binding is a process of finding a suitable handler in the target, which is resolved by static scoping at compiler-time, dynamic invocation chain at runtime, or both. • There are two methods to dynamically find a handler: stack unwinding and stack cutting. – Stack unwinding pops the stack frames to search for the matching exception handler – Stack cutting maintains a list of registered exception handlers and looks up the list for a suitable exception handler. Copyright@2013 Teddysoft
  22. 22. 6. Continuation • An exception continuation or exception model specifies the execution flow after an exception handler returns its control. –Termination model –Retry model –Resumption model Copyright@2013 Teddysoft
  23. 23. 容易搞混且重要的觀念:Fault, Error, Failure, Exception彼此的關係 fault error failure exception (1) design (2) component cause of failure a state may lead to failure service departs from specification represented by Copyright@2013 Teddysoft
  24. 24. 以下何者是design fault,何者是component fault? 1. 除以零 (division by zero) 2. Index Out of Bound 3. 網路斷線 4. 硬碟空間已滿 5. 檔案不存在 24 Copyright@2013 Teddysoft
  25. 25. 為什麼要區分design fault與 component fault?
  26. 26. Exception Handling vs. Fault-Tolerant Programming • Exception handling deals with component faults (anticipated exceptions) • Fault-tolerant programming deals with both component and design faults (anticipated and unanticipated exceptions) 26 Copyright@2013 Teddysoft 範圍不同、成本不同!
  27. 27. Java例外處理機制
  28. 28. Java Exception Handling: The try Statement (before JDK 7) 28Copyright@2013 Teddysoft
  29. 29. Java Exception Handling: The try_multi_catch in JDK 7 29Copyright@2013 Teddysoft
  30. 30. Java Exception Handling: The try_with_resources in JDK 7 30Copyright@2013 Teddysoft
  31. 31. Java Exception Class Hierarchy 31 checked unchecked IOException NullPointerException Throwable Exception RuntimeException Error IndexOutOfBoundsException SQLException Copyright@2013 Teddysoft
  32. 32. Use checked exceptions for recoverable conditions and run-time exceptions for programming errors
  33. 33. 使用Checked Exception須遵循Handle-or-Declare Rule Copyright@2013 Teddysoft handle declare
  34. 34. 程式如果違反Handle-or- Declare Rule將被Java Compiler 視為語法錯誤
  35. 35. 例外處理的4+1觀點 Usage (用途) Design (設計) Handling (處理) Tool-Support (工具支援) Process (流程)
  36. 36. 為什麼例外處理這麼難?
  37. 37. Usage View (例外用途觀點) Exception, 真的只是用來代表 「例外狀況」嗎?
  38. 38. 練習:分組討論要如何處理 EOFException與 InterruptedException?
  39. 39. EOFException範例 public void readDataFromFile(String aFileName){ try (DataInputStream input = new DataInputStream (new FileInputStream(aFileName))) { while (true) { System.out.print(input.readChar()); } } catch (EOFException e) { // How to "handle" this exception? } catch (IOException e) { e.printStackTrace(); } } Copyright@2013 Teddysoft
  40. 40. InterruptedException範例 public void sleepMillisecond(int ms){ try { Thread.sleep(ms); } catch (InterruptedException e) { // How to "handle" this exception? } } Copyright@2013 Teddysoft
  41. 41. Usage View • Failure • Notification – EOFException – InterruptedException Copyright@2013 Teddysoft
  42. 42. 案情沒有那麼單純,請看另一個 EOFException範例 Copyright@2013 Teddysoft public void fetchRawBytesAndSetupMessage(DataInputStream aIS) throws IOException, InvalidPacketException { int length = aIS.readInt(); setMessageLength(length); byte[] messageBody = new byte[length]; try { aIS.readFully(messageBody); } catch (EOFException e) { throw new InvalidPacketException("Data Underflow"); } setMessage(new String(messageBody)); }
  43. 43. Context 決定 exception的用途
  44. 44. Design View (例外設計觀點)
  45. 45. Design View • Declared: – 例外有宣告在元件的介面規範中 – 又稱為anticipated或expected例外 – 代表component fault • Undeclared: – 例外沒有宣告在元件的介面規範中 – 又稱為unanticipated或unexpected例外 – 代表design fault Copyright@2013 Teddysoft
  46. 46. Declared Exception Copyright@2013 Teddysoft public void fetchRawBytesAndSetupMessage(DataInputStream aIS) throws IOException, InvalidPacketException { int length = aIS.readInt(); setMessageLength(length); byte[] messageBody = new byte[length]; try { aIS.readFully(messageBody); } catch (EOFException e) { throw new InvalidPacketException("Data Underflow"); } setMessage(new String(messageBody)); }
  47. 47. Undeclared Exception (1/2) Copyright@2013 Teddysoft public Hamburg createHamburger(String type) { Hamburg ham = null; switch (type) { case "pork": ham = new SweetPorkHamburger(); break; case "beef": ham = new SweetBeefHamburger(); break; default: throw new RuntimeException ("Unsupported hamburger type:" + type); } return ham; }
  48. 48. Undeclared Exception (2/2) Copyright@2013 Teddysoft public void deposit(int value) { if (value < 0 ) { throw new IllegalArgumentException ("存款金額不得為負數."); } // doing normal deposit logic }
  49. 49. 你在做例外處理還是容錯設計? (1/2) Copyright@2013 Teddysoft public void deposit(int value) throws llegalArgumentException { if (value < 0 ) { throw new IllegalArgumentException ("存款金額不得為負數."); } // doing normal deposit logic } public void deposit(int value) { if (value < 0 ) { throw new IllegalArgumentException ("存款金額不得為負數."); } // doing normal deposit logic } D UC UCUD
  50. 50. 你在做例外處理還是容錯設計? (2/2) public String execute(String cmd) throws IOException, NullPointerException, IllegalArgumentException; Copyright@2013 Teddysoft UCD CD
  51. 51. Design View小結 撇開程式語言是否區分checked與 unchecked例外,唯有將例外宣告在介面 上(或以某種形式存在程式或文件中), 在設計階段程式設計師才有機會知道要 如何來因應可能會遭遇到的異常狀況。 Copyright@2013 Teddysoft
  52. 52. Handling View (例外處理觀點)
  53. 53. Handling View • Recoverability (可恢復性) – recoverable, unrecoverable(irrecoverable) • Exception handling constructs in languages and utilities – Roles, responsibilities, and collaborations (e.g., try, catch, finally in Java) Copyright@2013 Teddysoft
  54. 54. Handling View之 Recoverability
  55. 55. Recoverability:爽到你,艱苦到我 Copyright@2013 Teddysoft Thanks Linda
  56. 56. Recoverability:Callee與Caller都要負責任 Copyright@2013 Teddysoft Thanks Linda
  57. 57. public void sleepMillisecond(int ms){ try { Thread.sleep(ms); } catch (InterruptedException e) { // How to "handle" this exception? } } Recoverability思考練習1 Copyright@2013 Teddysoft Callee Caller: InterruptedException是一 個可以被修復的例外狀況嗎?
  58. 58. public void readDataFromFile(String aFileName){ try (DataInputStream input = new DataInputStream (new FileInputStream(aFileName))) { while (true) { System.out.print(input.readChar()); } } catch (EOFException e) { // How to "handle" this exception? } catch (IOException e) { e.printStackTrace(); } } Recoverability思考練習2 Copyright@2013 Teddysoft Callee Caller: EOFException與 IOException的recoverability?
  59. 59. public void fetchRawBytesAndSetupMessage(DataInputStream aIS) throws IOException, InvalidPacketException { int length = aIS.readInt(); setMessageLength(length); byte[] messageBody = new byte[length]; try { aIS.readFully(messageBody); } catch (EOFException e) { throw new InvalidPacketException("Data Underflow"); } setMessage(new String(messageBody)); } Recoverability思考練習3 Copyright@2013 Teddysoft Callee Caller: EOFException與 IOException的recoverability?
  60. 60. Handling View之 Exception Handling Constructs and Utilities
  61. 61. 不同的程式語言有不同的例外處理構件 • Java/C# – try-catch-finally • C++ – try-catch – destructor • Eiffel – Exception handlers in Eiffel are attached at the method level and all exceptions are caught by one handler. 61 Copyright@2013 Teddysoft
  62. 62. 重新思考try-catch-finally的責任與分工 • Try – Implement requirements (can have alternatives) – Prepare state recovery (e.g., make a check point) • Catch – Perform error and fault handling – Report exceptional conditions – Control retry flow • Finally – Release resources – Drop check points if any 62 Copyright@2013 Teddysoft
  63. 63. 例外處理也是一種程式設計,需要程式 設計能力與軟體元件支援 • 設計技巧 – Memento、Smart pointer、Check point、etc. – Exception hierarchy – EH best practices and patterns • 工具 – Logging (e.g., Log4j) – Common error formats and dialogs – EH smell detection – Marker & resolution 63 Copyright@2013 Teddysoft
  64. 64. Handling View小結 要判斷一個例外是否為一個可修復的狀 況,是例外處理「設計」的第一個步驟, 但這個判斷依據並不是一件容易的事。 確定了例外的recoverability之後,接著 可利用程式語言構件與軟體元件的協助 來實作例外處理程式碼。 Copyright@2013 Teddysoft
  65. 65. Tool-Support View (例外工具支援觀點)
  66. 66. Tool-Support View • Java語言的tool-support – 區分Checked與unchecked例外 Copyright@2013 Teddysoft
  67. 67. Java與C#程式比較 Copyright@2013 Teddysoft
  68. 68. Java語言的Tool-Support所造成的後遺症 • Interface evolution problem • Ignored checked exception 68 Copyright@2013 Teddysoft
  69. 69. Tool-Support View小結 為了提高軟體的強健度,開發人員 需要一個提醒機制,告知那些操作 有可能產生例外,否則開發人員更 容易忽略例外處理,只能等runtime 發生錯誤時再回頭修補。 Copyright@2013 Teddysoft
  70. 70. Process View (開發流程觀點)
  71. 71. Process View • Waterfall VS. IID (iterative and incremental development) • 如何在IID流程中規劃例外處理? – I will handle this exception when I have time.  Never happens! Copyright@2013 Teddysoft
  72. 72. 以Scrum為例 • Story – Normal scenarios – Failure scenarios • 這個sprint先做normal scenarios,下個 sprint再做failure scenarios Copyright@2013 Teddysoft 敏捷開發讓例外處理變得好簡單啊! 才怪
  73. 73. 「先做normal scenarios,再做failure scenarios」實務上有何問題? Copyright@2013 Teddysoft
  74. 74. 做normal scenarios的時候遇到例外 怎麼辦? Copyright@2013 Teddysoft public void fetchRawBytesAndSetupMessage(DataInputStream aIS) throws IOException, InvalidPacketException { int length = aIS.readInt(); setMessageLength(length); byte[] messageBody = new byte[length]; try { aIS.readFully(messageBody); } catch (EOFException e) { throw new InvalidPacketException("Data Underflow"); } setMessage(new String(messageBody)); }
  75. 75. Process View小結 IID或敏捷開發法不會讓例外處理變 得更簡單。若團隊沒有一套例外處 理設計規範,則很有可能反而會降 低系統的強健度。 Copyright@2013 Teddysoft
  76. 76. 例外處理的4+1種觀點結論
  77. 77. 例外處理…好難…Orz
  78. 78. 建立例外處理中心思想— Staged Robustness Model 例外處理的目標
  79. 79. 例外處理的目標
  80. 80. Robustness Levels (強健度等級) 80 Undefined Error- Reporting State- Recovery Behavior- Recovery 0 1 2 3 Robustness unpredictable All exceptions are reported State is correct under the presence of exceptions Service is delivered under the presence of exceptions Copyright@2013 Teddysoft
  81. 81. Robustness levels of components Element RL G0 RL G1 RL G2 RL G3 name undefined error-reporting state-recovery behavior-recovery service failing implicitly or explicitly failing explicitly failing explicitly delivered state unknown or incorrect unknown or incorrect correct correct lifetime terminated or continued terminated continued continued how- achieved NA (1) propagating all unhandled exceptions, and (2) catching and reporting them in the main program (1) error recovery and (2) cleanup (1) retry, and/or (2) design diversity, data diversity, and functional diversity also known as NA failing-fast weakly tolerant and organized panic strongly tolerant, self-repair, self- healing, resilience, and retry Copyright@2013 Teddysoft
  82. 82. Upgrading and degrading exception handling goals 82 fail-fast; keep user informed G0 G1 G2 restore state, clean up, and keep programs alive G3 attempt retries all retries fail state restoration or cleanup fail Copyright@2013 Teddysoft
  83. 83. Applicability for the robustness levels 83 RL Applicability G1  In the early stage of system development  Prototyping  Applying an evolutionary development methodology  Time-to-market G2  Outsourcing  Designing utility components used in different application domains  Behavior-recovery actions should be administered by the user G3  Developing mission critical systems  Designing components having sufficient application context to recover from behavioral failures, e.g., application controllers  Behavior-recovery actions are inappropriate to be administered by the user Copyright@2013 Teddysoft
  84. 84. 強健度等級小結
  85. 85. Consequences • Robustness without costly up-front design • Guiding exception handling implementation • Independent of languages Copyright@2013 Teddysoft
  86. 86. Bad Smells and Refactorings Refactoring基本觀念 Bad Smells and Refactorings
  87. 87. Refactoring基本觀念
  88. 88. What is Refactoring • Improving the internal structure of a software system without altering its external behavior [fowler] • Steps to perform refactoring: – Identifying code smells – Applying refactorings to remove the smells – Verifying satisfaction Copyright@2013 Teddysoft
  89. 89. Refactoring and EH Refactoring 89 Normal Behavior Exceptional Behavior Refactoring EH Refactoring Behavior Copyright@2013 Teddysoft
  90. 90. EH Smells, Refactorings, and RL 90 EH smell Refactoring RL Return code Replace Error Code with Exception G1 Ignored checked exception Replace Ignored Checked Exception with Unchecked Exception G1 Unprotected main program Avoid Unexpected Termination with Big Outer Try Block G1 Dummy handler Replace Dummy Handler with Rethrow G1 Nested try block Replace Nested Try Block with Method G2 Careless Cleanup Replace Careless Cleanup with Guaranteed Cleanup G2 Ignored checked exception Dummy handler Introduce Checkpoint Class G2 Spare handler Introduce Resourceful Try Clause G3Copyright@2013 Teddysoft
  91. 91. Smell: Return Code 91 Copyright@2013 Teddysoft public int withdraw(int amount) { if (amount > this.balance) return -1; else { this.balance = this.balance – amount; return this.balance; } }
  92. 92. Refactoring: Replace Error Code with Exception 92 Copyright@2013 Teddysoft public int withdraw(int amount) throws NotEnoughMoneyException { if (amount > this.balance) throw new NotEnoughMoneyException (); this.balance = this.balance – amount; }
  93. 93. Smell: Ignored Checked Exception 93 Copyright@2013 Teddysoft public void writeFile(String fileName, String data) { Writer writer = null; try { writer = new FileWriter(fileName); // may throw IOException writer.write(data); // may throw IOException } catch (IOException e) { // ignoring the exception } finally { // code for cleanup } }
  94. 94. Replace Ignored Checked Exception with Unchecked Exception public void writeFile(String fileName, String data) { Writer writer = null; try { writer = new FileWriter(fileName); /* may throw an IOException */ writer.write(data); /* may throw an IOException */ } catch (IOException e) { /* ignoring the exception */ } } ↓ public void writeFile(String fileName, String data) { Writer writer = null; try { writer = new FileWriter(fileName); /* may throw an IOException */ writer.write(data); /* may throw an IOException */ } catch (IOException e) { throw new UnhandledException(e, “message”); } } 94 Copyright@2013 Teddysoft
  95. 95. Smell: Unprotected Main Program 95 Copyright@2013 Teddysoft static public void main(String[] args) { MyApp myapp = new MyApp(); myapp.start(); }
  96. 96. Avoid Unexpected Termination with Big Outer Try Block 96 static public void main(String[] args) { MyApp myapp = new MyApp(); myapp.start(); } ↓ static public void main(String[] args) { try { MyApp myapp = new MyApp(); myapp.start(); } catch (Throwable e) { /* displaying and/or logging the exception */ } } Copyright@2013 Teddysoft
  97. 97. Smell: Dummy Handler 97 Copyright@2013 Teddysoft public void m(String aFileName) { try{ FileInputStream fis = new FileInputStream(new File(aFileName)); } catch(IOException e){ e.printStackTrace(); } finally{ // cleanup } }
  98. 98. Replace Dummy Handler with Rethrow 98 Copyright@2013 Teddysoft public void m(String aFileName) { try{ FileInputStream fis = new FileInputStream(new File(aFileName)); } catch(IOException e){ e.printStackTrace(); } finally{ // cleanup } } public void m(String aFileName) { try{ FileInputStream fis = new FileInputStream(new File(aFileName)); } catch(IOException e){ throw new UnhandledException (e, “message”); } finally{ // cleanup } }
  99. 99. Smell: Nested Try Statement 99 Copyright@2013 Teddysoft FileInputStream in = null; try { in = new FileInputStream(…); } finally { try { if (in != null) in.close (); } catch (IOException e) { /* log the exception */ } }
  100. 100. Replace Nested Try Statement with Method 100 FileInputStream in = null; try { in = new FileInputStream(…); } finally { try { if (in != null) in.close (); } catch (IOException e) { /* log the exception */ } } Copyright@2013 Teddysoft FileInputStream in = null; try { in = new FileInputStream(…); } finally { closeIO (in); } private void closeIO (Closeable c) { try { if (c != null) c.close (); } catch (IOException e) { /* log the exception */ } }
  101. 101. Smell: Careless Cleanup 101 Copyright@2013 Teddysoft public void cleanup String aFileName) { try{ FileInputStream fis = new FileInputStream(new File(aFileName)); fis.close(); } catch(IOException e){ throw new RuntimeException(e); } }
  102. 102. Replace Careless Cleanup with Guaranteed Cleanup 102 Copyright@2013 Teddysoft public void cleanup String aFileName) { try{ FileInputStream fis = new FileInputStream(new File(aFileName)); fis.close(); } catch(IOException e){ throw new RuntimeException(e); } } public void cleanup String aFileName) { FileInputStream fis = null; try{ fis = new FileInputStream(new File(aFileName)); } catch(IOException e){ throw new RuntimeException(e); } finally { closeIO(fis); } }
  103. 103. 練習:尋找 Smells
  104. 104. 練習:EH Refactoring
  105. 105. Advanced Refactoring
  106. 106. Introduce Checkpoint Class 106 public void foo () throws FailureException { try { /* code that may change the state of the object */ } catch (AnException e) { throw new FailureException(e); } finally {/* code for cleanup */} } ↓ public void foo () throws FailureException { Checkpoint cp = new Checkpoint (/* parameters */); try { cp. establish (); /* establish a checkpoint */ /* code that may change the state of the object */ } catch (AnException e) { cp.restore (); /* restore the checkpoint */ throw new FailureException(e); } finally { cp.drop(); } } Copyright@2013 Teddysoft
  107. 107. Smell: Spare handler 107 Copyright@2013 Teddysoft try { /* primary */ } catch (SomeException e) { try {/* alternative */} catch(AnotherException e) { throw new FailureException(e); } }
  108. 108. Introduce Resourceful Try Clause 108 try { /* primary */ } catch (SomeException e) { try {/* alternative */} catch(AnotherException e) { throw new FailureException(e); } } ↓ int attempt = 0; int maxAttempt = 2; boolean retry = false; do { try { retry = false; if (attempt == 0) { /* primary */ } else { /* alternative */ } } catch (SomeException e) { attempt++; retry = true; if (attempt > maxAttempt) throw new FailureException (e); } } while (attempt<= maxAttempt && retry) Copyright@2013 Teddysoft
  109. 109. 參考資料 Copyright@2013 Teddysoft
  110. 110. 複習 • 例外處理基本觀念 – EHM、fault、error、failure、exception • 例外處理的4+1觀點 • 建立例外處理中心思想—Staged Robustness Model • EH Bad Smells and Refactorings
  111. 111. 泰迪軟體敏捷開發訓練藍圖 Copyright@2013 Teddysoft
  112. 112. 謝謝,再見 XD
  • LaiSasha

    May. 6, 2021
  • tsengeagle

    Dec. 26, 2020
  • rettamkrad

    Aug. 27, 2020
  • chennelsonchen

    Oct. 25, 2019
  • kevinchentw

    Sep. 21, 2019
  • riversnow

    Oct. 6, 2017
  • ssuseree7b0b

    Oct. 1, 2017
  • ChengYouBai

    Jul. 9, 2017
  • gostlike

    Oct. 24, 2016
  • DorisHsu

    Sep. 19, 2016
  • paxtsai

    Jul. 23, 2016
  • am997vivian

    Apr. 15, 2016
  • Kalaexj

    Jan. 11, 2016
  • ShihHsuanHuang2

    Jan. 7, 2016
  • shuhungliou

    Nov. 17, 2015
  • franksmile0310

    Sep. 8, 2015
  • tyhs1414

    Sep. 3, 2015
  • ssuser143e05

    Jun. 11, 2015
  • russle1

    May. 25, 2015
  • ryanxhsieh

    May. 19, 2015

此為泰迪軟體在2013年9月14日舉辦的例外處理設計與重構班完整教材內容。

Vues

Nombre de vues

11 771

Sur Slideshare

0

À partir des intégrations

0

Nombre d'intégrations

7 029

Actions

Téléchargements

99

Partages

0

Commentaires

0

Mentions J'aime

30

×