티스토리 뷰


"Java Exceptions Interview Questions, Answers”라는 제목을 가진 재미있는 글이 있어 번역해 보았습니다.

Java에서 Exception이란?

프로그램이나 애플리케이션 수행 중에 발생하는 일정의 이벤트입니다. 모든 Java Exception은 “Exception”이라는 클래스의 자식 클래스입니다. Exception은 프로그램이 계속해서 수행될 수 있도록 적절히 처리할 수도 있습니다.

Exception 클래스의 상속구조는?

아래 그림처럼 Object 클래스를 상속받고 있는 Throwable부터 시작됩니다. 
 


Error란?

Exception과 유사합니다. 다만 프로그램에서 별도의 처리를 할 수 없다는 점이 그 차이점입니다.

Checked Exception과 Unchecked Exception의 차이는?

Checked Exception
Unchecked Exception
Exception 클래스의 자식 클래스
RuntimeException의 자식 클래스
별도의 처리를 하지 않는다면 컴파일러가 오류를 알려줌
컴파일러가 체크하지 않음
IOException처럼 외부 요인으로 발생
NullPointerException처럼 개발자 실수로 발생

RuntimeException을 Try~Catch문으로 처리할 수 있는지?

가능합니다. 하지만 RuntimeException을 Catch하지 않길 권장합니다. 만약 어떤 코드에서 RuntimeException이 발생할 수 있다면 발생하지 않도록 그 코드를 수정해야 합니다.

Checked Exception과 Unchecked Exception에는 어떤 것들이 있는지?

Checked : SQLException, IOException, FileNotFoundException, ClassNotFoundException, InterruptedException
Unchecked : ArithmethicException, NullPointerException, ClassCastException, NumberFormatException, IndexOutOfBoundsException

throw와 throws 구문의 차이점은?

throw는 Exception을 상위 메서드로 전달하고 throws는 메서드 선언시 어떤 Exception을 전달하는지 정의할 때 사용합니다.

finally 구문은 무엇인가?

finally 블록 안에 작성된 코드는 Exception이 발생하던지 하지 않던지 항상 실행됩니다.

catch문 없이 finally 구문을 사용할 수 있는지?

가능합니다. catch나 finally는 try 구문과 함께 사용됩니다.

try나 catch 구문 내에 return 코드가 있다면 finally 구문 내용이 실행되는지?

finally 구문은 항상 실행됩니다. return 코드는 finally 구문이 실행 된 뒤에 최종적으로 실행됩니다.

만약 System.exit()를 try 구문에서 사용하면 finally 구문 내용이 실행되는지?

실행되지 않습니다. 그 이유는 System.exit()은 JVM을 즉시 종료하라는 의미이기 때문에 finally를 실행하지 않고 JVM이 종료됩니다.

Exception 클래스의 메인 메서드는 무엇인지?

CheckedException이나 UncheckedException 중 어떤 Exception인지에 따라 차이가 있습니다.

아래 코드의 결과는?

public class TryCatchFinallyTrick {
    public static void main(String[] args) {
        System.out.println("The output is: " getName());
    }

    static int getName(){
        int a = 3;
        try{
            System.out.println("I am try");
            a=4;
            return a;
        } catch(Exception e){
            System.out.println("I am catch");
            a=5;
            return a;
        } finally {
            System.out.println("I am finally.");
            a = 6;
        }
    }
}
I am try
I am finally.
The output is: 4

아래 코드 결과는?

public class TryCatchFinallyTrick2 {
    public static void main(String[] args) {
        System.out.println("The output is: " getName());
    }

    static String getName(){
        String  name = "a";
        try{
            System.out.println("I am try");
            name = "try";
            return name;
        } catch(Exception e){
            System.out.println("I am catch");
            name = "catch";
            return name;
        } finally {
            System.out.println("I am finally.");
            name = "finally";
            return name;
        }
    }
}
I am try
I am finally.
The output is: finally




마지막 부분의 샘플코드 결과가 개인적으로 흥미로웠습니다. 조금만 생각해 보면 당연한 결과이긴 하나 갑자기 질문 받았을 때에는 조금 고민이 되는 코드일 수도 있을 거 같습니다.




공지사항
최근에 올라온 글
최근에 달린 댓글
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
글 보관함