Guava : Throwables
1. 메소드 목록
리턴 타입 | 메소드 | 설명 |
static List<Throwable> | getCausalChain(Throwable throwable) | throwable의 원인 사슬을 목록으로 반환한다. |
static <X extends Throwable> X | getCauseAs(Throwable throwable, Class<X> expectedCauseType) | expectedCauseType으로 변환해 throwable의 원인을 반환한다. |
static Throwable | getRootCause(Throwable throwable) | throwable의 루트 원인을 반환한다. |
static String | getStackTraceAsString(Throwable throwable) | throwable의 원인 스택 트레이스를 toString()을 사용해 문자열로 반환한다. |
static <X extends Throwable> void | propagateIfPossible(Throwable throwable, Class<X> declaredType) | 정확히 RuntimeException, Error나 declaredType의 인스턴스인 경우에만 예외를 전파한다. |
static <X1 extends Throwable, X2 extends Throwable> void | propagateIfPossible(Throwable throwable, Class<X1> declaredType1, Class<X2> declaredType2) | 정확히 RuntimeException, Error나 declaredType1, declaredType2의 인스턴스인 경우에만 예외를 전파한다. |
static <X extends Throwable> void | throwIfInstanceOf(Throwable throwable, Class<X> declaredType) | declaredType의 인스턴스인 경우에만 throwable을 던진다 |
static void | throwIfUnchecked(Throwable throwable) | RuntimeException이나 Error의 경우에만 throwable을 던진다. |
2. 메소드 설명
1. throwIfInstanceOf : declaredType의 인스턴스인 경우에만 throwable을 던진다
for (Foo foo : foos) {
try {
foo.bar();
} catch (BarException | RuntimeException | Error t) {
failure = t;
}
}
if (failure != null) {
throwIfInstanceOf(failure, BarException.class);
throwIfUnchecked(failure);
throw new AssertionError(failure);
}
2. throwIfUnchecked : RuntimeException이나 Error의 경우에만 throwable을 던진다.
for (Foo foo : foos) {
try {
foo.bar();
} catch (RuntimeException | Error t) {
failure = t;
}
}
if (failure != null) {
throwIfUnchecked(failure);
throw new AssertionError(failure);
}
3. propagateIfPossible : 정확히 RuntimeException, Error나 declaredType의 인스턴스인 경우에만 예외를 전파한다.
try {
someMethodThatCouldThrowAnything();
} catch (IKnowWhatToDoWithThisException e) {
handle(e);
} catch (Throwable t) {
Throwables.propagateIfPossible(t, OtherException.class);
throw new RuntimeException("unexpected", t);
}
3. 동작 원리
public void doSomething()
throws IOException, SQLException {
try {
someMethodThatCouldThrowAnything();
} catch (IKnowWhatToDoWithThisException e) {
handle(e);
} catch (Throwable t) {
Throwables.propagateIfInstanceOf(t, IOException.class);
Throwables.propagateIfInstanceOf(t, SQLException.class);
throw Throwables.propagate(t);
}
}
public void doSomething()
throws IOException, SQLException {
try {
someMethodThatCouldThrowAnything();
} catch (IKnowWhatToDoWithThisException e) {
handle(e);
} catch (SQLException ex) {
throw ex;
} catch (IOException ex) {
throw ex;
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
참고
- https://guava.dev/releases/snapshot/api/docs/com/google/common/base/Throwables.html#propagateIfPossible-java.lang.Throwable-java.lang.Class-
- https://stackoverflow.com/questions/5090417/how-do-i-use-throwables-propagateifinstanceof-from-google-guava
Author And Source
이 문제에 관하여(Guava : Throwables), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@jsj3282/Guaba-Throwables저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)