Item 9.Prefer try-with-resources to try-finally
아이템 9: try-finally 보다는 try-with-resources를 사용하라
try-finally의 문제점
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("test.txt"));
// 파일 읽기
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}try-with-resources의 장점
자바에서의 리소스 관리
try-with-resources를 사용한 예시
결론
PreviousItem 8. Avoid Finalizers and CleanersNextItem10. Adhering to General Rules When Overriding equals
Last updated