9. try-finalliy 보다는 try-with-resources를 사용하라
2015 단어 Effective JavaEffective Java
try-finally
static String firstLineOfFile(String path) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}
}
- try-finally 는 전통적인 자원 회수 방식이다.
- 예외가 발생하거나 메서드에서 반환되는 경우까지도포함되어서 사용되었다.
static void copy(String src, String dst) throws IOException {
InputStream in = new FileInputStream(src);
try {
OutputStream out = new FileOutputStream(dst);
try {
byte[] buf = new byte[BUFFER_SIZE];
int n;
while ((n = in.read(buf)) >= 0) {
out.write(buf, 0, n);
}
} finally {
out.close();
}
} finally {
in.close();
}
}
- 다만, 자원이 둘 이상이 되면 지저분해 진다는 단점이 있다.
- 예외는 try 블록과 finally 블록 모두에서 발생할 수 있다. 물리적인 문제를 이유로 firstLineOfFile 메서드 안의 readLine 메서드가 예외를 던지고, 같은 이유로 close 메서드도 실패할 것이다.
- 이런 상황이면, 두번째 예외가 첫번째 예외를 완전히 집어삼킨다.
- 그러면 첫번째 예외에 대한 정보는 스탯 추적 내역에 남지 않게 된다.
try-with-resources
static String firstLineOfFile(String path) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.readLine();
}
}
- try-with-resources 는 자원을 회수하는 최선책이다.
- 이 구조를 사용하려면 해당 자원이 AutoCloseable 인터페이스를 구현해야 한다.
static void copy(String src, String dst) throws IOException {
try (InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst)) {
byte[] buf = new byte[BUFFER_SIZE];
int n;
while ((n = in.read(buf)) >= 0) {
out.write(buf, 0, n);
}
}
}
- 복수의 자원을 회수하는 try-with-resources 는 readLine 과 close 호출 양쪽에서 예외가 발생하면 close 에서 발생한 예외는 숨겨지고 readLine 에서 발생한 예외가 기록된다.
- 숨겨진 예외들에 대해서도 스탯 추적 내역에 '숨겨졌다(suppressed)'라는 꼬리표를 달고 출력된다.
꼭 회수해야 하는 자원인 경우, 무조건 try-with-resources를 사용하자.
Author And Source
이 문제에 관하여(9. try-finalliy 보다는 try-with-resources를 사용하라), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@chullll/9.-try-finalliy-보다는-try-with-resources를-사용하라저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)