差異處
這裏顯示兩個版本的差異處。
java:effective_java:exceptions:checked_vs_unchecked [2022/04/12 22:39] tony [Introduction] |
java:effective_java:exceptions:checked_vs_unchecked [2023/06/25 09:48] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | {{tag>java effective_java exceptions}} | ||
- | ====== Effective Java - Use checked exceptions for recoverable conditions and runtime exceptions for programming errors (ToDo) ====== | ||
- | ===== Introduction ===== | ||
- | 這個Item在強調例外的使用時機,包含 | ||
- | * Checked Exceptions應用在recoverable conditions | ||
- | * Unchecked(Runtime) Exception應用在programming errors | ||
- | 下一個問題就是怎麼定義recoverable conditions與programming errors。我分享[[https://www.books.com.tw/products/0010637503|笑談軟體工程:例外處理設計的逆襲]]中的觀點: | ||
- | * Programming errors: design fault造成的,通常代表是bug;因為是bug,所以retry 1億次也還是錯的。 | ||
- | * Recoverable conditions: component fault造成的,可能是軟體元件、硬體元件突然發生故障,或與其溝通的過程中發生故障而導致的暫態缺陷,透過retry是有機會修復的。如果是永久缺陷,可能就需要排除原因才有機會執行成功。 | ||
- | 根據上面的定義,就可以知道什麼時候該拋checked exception,什麼時候該拋unchecked exception了。\\ | ||
- | \\ | ||
- | 除此之外,這個Item還有提到幾個設計要注意的事情: | ||
- | * 按照慣例,Error應保留給JVM使用,用以表明程序無法繼續執行的情況。所以開發人員通常不該去Extend Error。 | ||
- | * 例外本身是有著設計上的代表意義,可以在類別上定義能供給Client做進一步處理的介面。不建議Client使用例外本身的字串訊息去做例外處理,因為字串訊息說變就變,程式容易出問題。 | ||
- | ===== Note ===== | ||
- | Effective Java第三版Item 70。 | ||
- | ===== Reference ===== | ||
- | * Effective Java, 3/e | ||
- | * [[http://teddy-chen-tw.blogspot.com/2011/05/checked-or-unchecked-exceptions-1.html|搞笑談軟工 - Checked or unchecked exceptions (1)]] | ||
- | ===== ===== | ||
- | ---- | ||
- | \\ | ||
- | ~~DISQUS~~ |