-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
Description
public class ErrorStackTrace implements AutoCloseable{
@Override
public void close() throws RuntimeException{
System.out.println("close");
throw new IllegalStateException();
}
public void hello() {
System.out.println("hello");
throw new IllegalStateException();
}이 코드는 close와 hello를 호출 시 런타임 예외 중 하나인 IllegalStateException이 발생하게 된다. 이 코드를 main에서 try-catch-finally와 try-with-resuources로 돌려보겠다.
- try-catch-finally
public static void main(String[] args) {
ErrorStackTrace errorStackTrace = null;
try {
errorStackTrace = new ErrorStackTrace();
errorStackTrace.hello();
} finally {
if (errorStackTrace != null) {
errorStackTrace.close();
}
}
}- try-with-resources
public static void main(String[] args) {
try (ErrorStackTrace errorStackTrace = new ErrorStackTrace()) {
errorStackTrace.hello();
}
}이렇게 try-catch-finally에서는 hello의 오류가 찍히지 않는 것을 볼 수 있고 이를 에러 스택 트레이스 누락이라고 한다.
공부하다가 이런 경우를 봤는데 try-catch-finally에서 저런 에러스택트레이스 누락이 발생하여 디버깅이 어려울 수 있으니 try-with-resources를 사용하자. 라고 하더라구요. 근데 저런 에러스택트레이스 누락이 어떤 원리로 발생하는지 모르겠어서 남겨봅니당..
gmelon

