Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

搞懂Java例外處理的難題:Checked與Unchecked Exceptions不再是問題

5,160 views

Published on

討論Java語言中checked與unchecked exceptions使用上的問題

  • Be the first to comment

搞懂Java例外處理的難題:Checked與Unchecked Exceptions不再是問題

  1. 1. Teddy Chenteddy@teddysoft.tw Feb. 21 2013
  2. 2. Copyright@2013 Teddysoft
  3. 3. 系統不穩定 3
  4. 4. 4
  5. 5. 5
  6. 6. 6
  7. 7. Problem 7
  8. 8. Problem 8
  9. 9. Copyright@2013 Teddysoft
  10. 10. • Correctness – Contract Specification(超出範圍)• Robustness – Exception Handling Copyright@2013 Teddysoft
  11. 11. Copyright@2013 Teddysoft
  12. 12. CheckedException Throwable Unchecked Exception Exception Error RuntimeException Copyright@2013 Teddysoft
  13. 13. declarehandle Copyright@2013 Teddysoft
  14. 14. Copyright@2013 Teddysoft
  15. 15. Copyright@2013 Teddysoft
  16. 16. • Checked exceptions for recoverable conditions – 打死不知道要如何去recover 任何東東?class FileService {… backup () { FileService fs = new public copy (String s) FileService(); throws IOException { try { … fs.copy(“a.txt”); if (error) } throw new catch (IOException e){ IOException(“reason”); // exception handling code } }} } Copyright@2013 Teddysoft
  17. 17. 123 Copyright@2013 Teddysoft
  18. 18. 1 2 3Copyright@2013 Teddysoft
  19. 19. • 我就是不喜歡checked exception,咬我啊!• 例如,Standard Widget Toolkit (SWT, Eclipse的UI元件)只使用unchecked exception: – SWTException用來表示可修復的SWT錯誤。 – SWTError則用來表示不可修復的錯誤。 Copyright@2013 Teddysoft
  20. 20. YourapplicationSpring DataAccessException uncheckedFramework checked Java SQLException HibernateException technology- technology- specific specific Copyright@2013 Teddysoft
  21. 21. Copyright@2013 Teddysoft
  22. 22. • Exception Type• Recoverability• Application Context• Robustness Level – G1: Error-reporting (錯誤回報) – G2: State-recovery (狀態回復) – G3: Behavior-recovery (行為回復)• Exception Handling Policy Copyright@2013 Teddysoft
  23. 23. Copyright@2013 Teddysoft
  24. 24. Copyright@2013 Teddysoft
  25. 25. 練習1: 如果Accepter收到一 MessagePresentationLayer E 5. report 個 IOException,鄉民們第 一個反應是什麼? AppWinApplication E 4. wrapLayer GameServerService Layer 3. recover E 2. rethrow 練習2: 如果Accepter收到 Acceptor 一個 RuntimeException, 鄉民們第一個反應是什麼? JVM E 1. throw Copyright@2013 Teddysoft
  26. 26. Message 練習3: 如果Accepter收到一Presentation E 5. report 個 IOException,鄉民們該Layer AppWin 如何處置?Application E 4. wrapLayer GameServer 3. recoverService Layer E 2. rethrow 練習4: 如果Accepter收到 Acceptor 一個 RuntimeException, JVM E 1. throw 鄉民們該如何處置? Copyright@2013 Teddysoft
  27. 27. Message 練習5: 如果Accepter收到一Presentation E 5. report 個 IOException,鄉民們該Layer AppWin 如何處置?Application E 4. wrapLayer GameServer 3. recover 練習6: 如果GameServer收Service Layer E 2. rethrow Acceptor 到 IOException ,鄉民們該 如何處置? JVM E 1. throw Copyright@2013 Teddysoft
  28. 28. MessagePresentationLayer E 5. report 練習7: Accepter的RL要 AppWin E 4. wrap 設為多少?ApplicationLayer GameServer 3. recoverService Layer E 2. rethrow 練習8: GameServer的RL Acceptor 要設為多少? JVM E 1. throw Copyright@2013 Teddysoft
  29. 29. MessagePresentationLayer E 5. report 練習9: Acceptor這一層 AppWin 有哪些policy可用?Application E 4. wrapLayer GameServer 3. recoverService Layer E 2. rethrow Acceptor 練習10: GameServer這 JVM 一層有哪些policy可用? E 1. throw Copyright@2013 Teddysoft
  30. 30. Copyright@2013 Teddysoft 30

×