异常(Exceptions)

仅仅在异常状况下才使用Exceptions
  • 不要把Exceptions用于ordinary control flow
  • 不要写这样的API: 迫使别人把Exceptions用于ordinary control flow
  • 应该写这样的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时,有哪些准则?
  • throw checked exceptions for recoverable conditions
  • 为你的checked exceptions提供methods,便于caller recoverable
  • throw unchecked exceptions for programming errors
  • 只定义checked exceptions 或 runtime exceptions,不要定义Error及其子类或其他的throwable及其子类
  • 不知道throw哪种exceptions时,就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应该包含导致该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是区分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
  • 不要过度使用exception translation和exception chaining
  • 不要忽视异常,在try catch中,一个空的catch块破坏了exception的目的
对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(...);
}
对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);
    }
}
说说对原子性失败(failure-atomic)的理解
  • 一次失败的方法调用应该使该对象处于调用之前的状态,具有这种属性的方法,称为failure-atomic
  • 使用failure atomicity时,不应该significantly 增加代码的cost和complexity
如何实现failure-atomic效果?
  • design immutable objects
  • 在修改对象状态之前,先计算并check parameters的正确性
  • 临时copy object,在copy object 计算完成,然后替换掉原来object的内容
  • 写recovery code,使该object的状态roll back到原始状态

你可能感兴趣的:(异常(Exceptions))