Item 73. Abstraction Level Exceptions
μΆμν μμ€μ λ§λ μμΈλ₯Ό λμ§λΌ
μννλ €λ μΌκ³Ό κ΄λ ¨ μμ΄ λ³΄μ΄λ μμΈκ° λ°μνλ©΄ μ΄λ€ μλ¬κ° λ°μνλμ§ μκΈ° μ΄λ €μμ§λ€.
μμΈ λ²μ(exception translation)
μμ κ³μΈ΅μμ μ¬μ©ν λ, μ μμ€ μμΈλ₯Ό μ‘μ μμ μ μμ€μ λ§λ μμΈλ‘ λ°κΏ λμ§λ μμΈ λ²μ(exception translation) κΈ°λ²μ μ¬μ©νμ¬ μλ¬ νμ μ μ½κ² ν μ μλ€.
class Example {
public static void main(String[] args) {
try {
// ...
} catch (LowerLevelException e) {
// μΆμν μμ€μ λ§κ² λ€λ₯Έ μμΈλ‘ μ ν
throw new HigherLevelException();
}
}
}μ€μ λ‘ AbstractSequentialListμ get λ©μλλ μλμ κ°μ΄ ꡬνλμ΄ μλ€.
public abstract class AbstractSequentialList<E> extends AbstractList<E> {
public E get(int index) {
try {
return listIterator(index).next();
} catch (NoSuchElementException exc) {
throw new IndexOutOfBoundsException("Index: " + index);
}
}
}μμΈ μ°μ(exception chaining)
λ§μ½ μ μμ€ μμΈκ° μ μ©ν μ 보λ₯Ό λ΄κ³ μλ€λ©΄, μμΈ μ°μ(exception chaining) κΈ°λ²μ μ¬μ©νμ¬ μμΈ μ°κ²°μ ν΅ν΄ μ μμ€ μμΈμ μ 보λ₯Ό κ³ μμ€ μμΈμ λ΄μμ λμ§λ λ°©λ²λ μλ€.
μμ κ°μ΄ μ€μ΄λ³΄λ΄κ² λλ©΄ getCause λ©μλλ₯Ό ν΅ν΄ μ μμ€ μμΈλ₯Ό νμΈν μ μμ΄, μμΈκ³Ό κ³ μμ€ μμΈμ μ€ν μΆμ μ 보λ₯Ό λͺ¨λ νμΈν μ μκ² λλ€.
Last updated
Was this helpful?