Java 기본 과정

예외 II

예외의 강제
 
API를 사용할 때 설계자의 의도에 따라서 예외를 반드시 처리해야 하는 경우가 있다. 아래의 예제를 보자.
 
package org.opentutorials.javatutorials.exception;
import java.io.*;
public class CheckedExceptionDemo {
    public static void main(String[] args) {
        BufferedReader bReader = new BufferedReader(new FileReader("out.txt"));
        String input = bReader.readLine();
        System.out.println(input); 
    }
}
어려운 코드다. 하지만 지금의 맥락에서는 중요한 내용이 아니다. 그래서 예외와 관련되지 않은 부분에 대한 자세한 설명은 하지 않겠다. 그냥 out.txt 파일을 읽어서 그것을 화면에 출력하는 내용이라고만 이해하자. 이 코드를 실행시키려면 out.txt 파일을 프로젝트의 루트 디렉토리에 위치시켜야 한다.  
 
위의 코드를 컴파일해보면 아래와 같은 에러가 발생하면서 컴파일이 되지 않을 것이다.
 
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Unhandled exception type FileNotFoundException
    Unhandled exception type IOException
    at org.opentutorials.javatutorials.exception.CheckedExceptionDemo.main(CheckedExceptionDemo.java:5)
 
이것은 아래 로직에 대한 예외처리가 필요하다는 뜻이다.
 
new FileReader("out.txt")
FileReader라는 클래스를 API문서에서 찾아보자. FileReader의 생성자를 문서에서 찾아보면 아래와 같은 부분이 있다.
 
 
Throws는 한국어로는 '던지다'로 번역된다. 위의 내용은 생성자 FileReader의 인자 fileName의 값에 해당하는 파일이 디렉토리이거나 어떤 이유로 사용할 수 없다면 FileNotFoundException을 발생시킨다는 의미다.
 
이것은 FileReader의 생성자가 동작할 때 파일을 열 수 없는 경우가 생길 수 있고, 이런 경우 생성자 FileReader에서는 이 문제를 처리할 수 없기 때문에 이에 대한 처리를 생성자의 사용자에게 위임하겠다는 의미다. 그것을 던진다(throw)고 표현하고 있다. 따라서 API의 사용자 쪽에서는 예외에 대한 처리를 반드시 해야 한다는 의미다. 따라서 아래와 같이 해야 FileReader 클래스를 사용할 수 있다.
 
 
 
package org.opentutorials.javatutorials.exception;
import java.io.*;
public class CheckedExceptionDemo {
    public static void main(String[] args) {
        BufferedReader bReader = null; 
        try {
            bReader = new BufferedReader(new FileReader("out.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String input = bReader.readLine();
        System.out.println(input); 
    }
}
 
BufferedReader 클래스의 readLine 메소드는 IOException을 발생시킬 수 있다. 다시 아래와 같이 코드를 수정하자.
 
        try{
            String input = bReader.readLine();
        } catch (IOException e){
            e.printStackTrace();
        }       
 
 
throw와 throws
 
지금까지 예외를 처리하는 방법으로 try...catch...finally를 배웠다. 이외에 다른 방법도 있다.  throw를 사용하는 것이다. throw는 예외처리를 다음 사용자에게 넘기는 것이다. 다음 사용자는 누구일까? 코드를 보자.
 
 
 
 
 
package org.opentutorials.javatutorials.exception;
import java.io.*;
class B{
    void run(){
        BufferedReader bReader = null;
        String input = null;
        try {
            bReader = new BufferedReader(new FileReader("out.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try{
            input = bReader.readLine();
        } catch (IOException e){
            e.printStackTrace();
        }       
        System.out.println(input); 
    }
}
class C{
    void run(){
        B b = new B();
        b.run();
    }
}
public class ThrowExceptionDemo {
    public static void main(String[] args) {
         C c = new C();
         c.run();
    }   
}
 
ThrowExceptionDemo.main()은 C.run()의 사용자이다. C.run()은 B.run()의 사용자이다. 반대로 B.run()의 다음 사용자는 C.run()이고 C.run()의 다음 사용자는 ThrowExceptionDem.main()이 되는 셈이다. 
 
위의 코드는 B.run()이 FileReader의 생성자와 BufferedReader.readLine가 던진 예외를 try...catch로 직접 처리한다. 즉 B.run이 예외에 대한 책임을 지고 있다.
 
그런데 B.run이 예외 처리를 직접 하지 않고 다음 사용자 C.run()에게 넘길 수 있다. 아래의 코드를 보자.
 
 
 
 
package org.opentutorials.javatutorials.exception;
import java.io.*;
class B{
    void run() throws IOException, FileNotFoundException{
        BufferedReader bReader = null;
        String input = null;
        bReader = new BufferedReader(new FileReader("out.txt"));
        input = bReader.readLine();
        System.out.println(input); 
    }
}
class C{
    void run(){
        B b = new B();
        b.run();
    }
}
public class ThrowExceptionDemo {
    public static void main(String[] args) {
         C c = new C();
         c.run();
    }   
}
 
 
B 내부의 try...catch 구문은 제거되었고 run 옆에 throws IOException, FileNotFoundException이 추가되었다. 이것은 B.run 내부에서 IOException, FileNotFoundException에 해당하는 예외가 발생하면 이에 대한 처리를 B.run의 사용자에게 위임하는 것이다. 위의 코드에서 B.run의 사용자는 C.run이다. 따라서 C.run은 아래와 같이 수정돼야 한다.
 
class C{
    void run(){
        B b = new B();
        try {
            b.run();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
 
이 책임을 다시 main에게 넘겨보자.
 
 
 
class C{
    void run() throws IOException, FileNotFoundException{
        B b = new B();
        b.run();
    }
}
public class ThrowExceptionDemo {
    public static void main(String[] args) {
         C c = new C();
         try {
            c.run();
        } catch (FileNotFoundException e) {
            System.out.println("out.txt 파일은 설정 파일 입니다. 이 파일이 프로잭트 루트 디렉토리에 존재해야 합니다.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }   
}
 
out.txt 파일을 찾을 수 없는 상황은 B.run 입장에서는 어떻게 할 수 있는 일이 아니다. 엔드유저인 애플리케이션의 사용자가 out.txt 파일을 루트 디렉토리에 위치시켜야 하는 문제이기 때문에 애플리케이션의 진입점인 메소드 main으로 책임을 넘기고 있다.
 
예외 처리는 귀찮은 일이다. 그래서 예외를 다음 사용자에게 전가(throw)하거나 try...catch로 감싸고 아무것도 하지 않고 싶은 유혹에 빠지기 쉽다. 하지만 예외는 API를 사용하면서 발생할 수 있는 잠재적 위협에 대한 API 개발자의 강력한 암시다. 이 암시를 무시해서는 안 된다. 물론 더욱 고민스러운 것은 예외 처리 방법에 정답이 없다는 것이겠지만 말이다.

댓글

댓글 본문
graphittie 자세히 보기