Exception Translation & Exception Chaining

// Exception Translation
try {
    // Use lower-level abstraction to do our bidding
    ...
} catch(LowerLevelException e) {
    throw new HigherLevelException(...);
}

 

// Exception Chaining
try {
    ... // Use lower-level abstraction to do our bidding
} catch (LowerLevelException cause) {
    throw new HigherLevelException(cause);
}

 

In summary, if it isn’t feasible to prevent or to handle exceptions from lower
layers, use exception translation, unless the lower-level method happens to guarantee
that all of its exceptions are appropriate to the higher level. Chaining provides
the best of both worlds: it allows you to throw an appropriate higher-level
exception, while capturing the underlying cause for failure analysis (Item 63).

你可能感兴趣的:(exception)