異常(Exceptions)

僅僅在異常狀況下才使用Exceptions
  • 不要把Exceptions用于ordinary control flow
  • 不要寫(xiě)這樣的API: 迫使別人把Exceptions用于ordinary control flow
  • 應(yīng)該寫(xiě)這樣的API:
have a separate “state-testing” method indicating 
whether it is appropriate to invoke the state-dependent method
比如:
the Iterator interface has the state-dependent method next 
and the corresponding state-testing method hasNext 
have the state-dependent method return an empty optional 
or a distinguished value such as null 
if it cannot perform the desired computation
java throwable有哪些?
checked exceptions, runtime exceptions, errors
其中 runtime exceptions, errors 都屬于unchecked exceptions
在使用java的throwable時(shí),有哪些準(zhǔn)則?
  • throw checked exceptions for recoverable conditions
  • 為你的checked exceptions提供methods,便于caller recoverable
  • throw unchecked exceptions for programming errors
  • 只定義checked exceptions 或 runtime exceptions,不要定義Error及其子類(lèi)或其他的throwable及其子類(lèi)
  • 不知道throw哪種exceptions時(shí),就throw RuntimeException
  • Avoid unnecessary use of checked exceptions,一種消除 a checked exception的方式是return an optional of the desired result type
  • 盡量document all exceptions thrown by each method
  • exception的detail message應(yīng)該包含導(dǎo)致該exception的所有parameters 和 fields 的值,比如IndexOutOfBoundsException的detail message包括 the lower bound, the upper bound, and the index value that failed to lie between the bounds
為什么擁抱使用standard exceptions
  • 能否獲得高度的code reuse是區(qū)分expert programmers和一般programmers的重要屬性之一
  • Reusing standard exceptions能使你的API更容易learn, use, and read
  • fewer exception classes意味著smaller memory footprint 和 less time spent loading classes
  • 不要直接 reuse: Exception, RuntimeException, Throwable, or Error
  • 常被reused exceptions: IllegalArgumentException,IllegalStateException,NullPointerException,IndexOutOfBoundsException,ConcurrentModificationException,UnsupportedOperationException
  • 不要過(guò)度使用exception translation和exception chaining
  • 不要忽視異常,在try catch中,一個(gè)空的catch塊破壞了exception的目的
對(duì)exception translation的理解
  • higher layers should catch lower-level exceptions and, in their place, throw exceptions that can be explained in terms of the higher-level abstraction
// Exception Translation
try {
    ... // Use lower-level abstraction to do our bidding
} catch (LowerLevelException e) {
    throw new HigherLevelException(...);
}
對(duì)exception chaining的理解
  • The lower-level exception (the cause) is passed to the higher-level exception
// Exception Chaining
try {
    ... // Use lower-level abstraction to do our bidding
} catch (LowerLevelException cause) {
    throw new HigherLevelException(cause);
}
// Exception with chaining-aware constructor
class HigherLevelException extends Exception {
    HigherLevelException(Throwable cause) {
        super(cause);
    }
}
說(shuō)說(shuō)對(duì)原子性失敗(failure-atomic)的理解
  • 一次失敗的方法調(diào)用應(yīng)該使該對(duì)象處于調(diào)用之前的狀態(tài),具有這種屬性的方法,稱(chēng)為failure-atomic
  • 使用failure atomicity時(shí),不應(yīng)該significantly 增加代碼的cost和complexity
如何實(shí)現(xiàn)failure-atomic效果?
  • design immutable objects
  • 在修改對(duì)象狀態(tài)之前,先計(jì)算并check parameters的正確性
  • 臨時(shí)copy object,在copy object 計(jì)算完成,然后替換掉原來(lái)object的內(nèi)容
  • 寫(xiě)recovery code,使該object的狀態(tài)roll back到原始狀態(tài)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評(píng)論 0 10
  • 異常類(lèi)(Exception Classes) Kotlin中所有的異常類(lèi)都是Throwable的子類(lèi)。每個(gè)異常都有...
    已遷至知乎_此不再維護(hù)閱讀 3,890評(píng)論 0 0
  • 世間若是沒(méi)有薄情之人,何來(lái)寡義之閡? 世間若是一篇和睦之象,何來(lái)死活之爭(zhēng)? 似乎我像是深憂(yōu)天下的騷客,揣著一肚子的...
    夙音閱讀 329評(píng)論 1 1
  • 雨后的天空明鏡似的,空氣中還帶著絲絲甜味。下班時(shí)間,街上人來(lái)人往,奔向各自目的地。其中有一位姑娘顯的特別引人...
    曉卉669閱讀 266評(píng)論 0 0
  • 我不喜歡你非讓我理解是不是有點(diǎn)強(qiáng)人所難。
    酸奶兩個(gè)閱讀 157評(píng)論 0 0

友情鏈接更多精彩內(nèi)容