티스토리 뷰

Programing/Java

Exception Propagation

Tomining 2016. 7. 28. 23:00
팀 코드 리뷰를 하면서 나왔던 의문을 팀원이 정리한 내용을 바탕으로 정리한 내용입니다.

들어가며...

try {
    doSomething();
    System.out.println("Normal statement.");
} finally {
    System.err.println("From finally block.");
}

위와 같은 코드에서 doSomething() 메서드 수행시 Exception 이 발생하면 어떻게 될까라는 의문이 생겼습니다.
catch문이 별도로 존재하지 않아 Exception 은 상위로 throw 가 될까요? 아니면 skip 되는 것일까요?


Exception Propagation



Java 에서 Exception 이 발생했을 경우 Exception Handler 가 처리하게 되는데, Exception Handler 를 찾아가는 방식이 위 그림과 같습니다.

call stack 을 이용하여 역방향으로 Exception Handler 를 탐색해가게 됩니다.
탐색하면서 Exception Handler 가 처리할 수 있는 Exception 타입인지 체크 한 뒤 맞다면 처리하고 아니라면 상위로 전달됨을 볼 수 있습니다.
즉, 발생한 Exception 에 대해서 처리가 가능한 Handler 를 찾을 때까지 상위로 전달되면서 찾아가게 됩니다.
적절한 Handler 를 찾지 못한다면 JVM 까지 전달되며, 최종적으로 JVM 이 Exception 을 처리하게 됩니다.


Sample Code


public class ExceptionPropagationExample {
    public static void main(String[] args) {
        System.out.println("1. Main start");
        ExceptionPropagationExample example = new ExceptionPropagationExample();

        try {
            example.doSomething();
        } catch (Exception e) {
            System.out.println("5. Catch exception");
            e.printStackTrace();
        }
    }

    private void doSomething() {
        System.out.println("2. execute something");
        try {
            occursException();
        } finally {
            System.out.println("4. finally block");
        }
    }

    private void occursException() {
        System.out.println("3. Exception occurs");
        String code = null;
        code.length();  //NPE occurs
    }
}
1. Main start
2. execute something
3. Exception occurs
4. finally block
5. Catch exception
java.lang.NullPointerException
    at tomining.exception.ExceptionPropagationExample.occursException(ExceptionPropagationExample.java:31)
    at tomining.exception.ExceptionPropagationExample.doSomething(ExceptionPropagationExample.java:22)
    at tomining.exception.ExceptionPropagationExample.main(ExceptionPropagationExample.java:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

  1. main() 호출
  2. doSomething() 호출
  3. occursException() 호출
  4. NullPointException 발생
  5. doSomething() 의 finally 구문 수행 —> 상위 method 인 main() 함수로 Exception 전달
  6. main() 함수의 catch 구문 수행


결론

발생한 Exception 을 적절한 catch 구문(Exception Handler) 을 만날 때까지 상위로 전달되며, 최종적으로 JVM 까지 전달되어 처리될 수 있습니다.
따라서 예상가능한 Exception 에 대해서는 꼭 catch 구문을 통해서 적절한 처리를 해야할 거 같습니다.


참고

'Programing > Java' 카테고리의 다른 글

논리 연산자(||, &&)와 비트 연산자(|, &)  (0) 2016.09.28
IBATIS CacheModel  (0) 2016.09.26
Java 로그에서 StackTrace 가 생략되는 현상  (0) 2016.04.04
json parser 정리  (0) 2016.04.04
Java DBCP Datasource 설정  (0) 2016.02.23
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함