僅僅在異常狀況下才使用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)
最后編輯于 :2018.03.03 17:21:19
?著作權(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ù)。