Study/Effective Java
์์ดํ 9. try-finally๋ณด๋ค๋ try-with-resources๋ฅผ ์ฌ์ฉํ๋ผ
sw_develop
2023. 5. 6. 18:56
๐์ํฉ
- ์๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์๋ close ๋ฉ์๋๋ฅผ ํธ์ถํด ์ง์ ๋ซ์์ค์ผ ํ๋ ์์์ด ์กด์ฌํจ
- ex) InputStream, OutputStream, java.sql.Connection
- ์ด๋ฌํ ์์ ๋ซ๊ธฐ๋ ํด๋ผ์ด์ธํธ๊ฐ ๋์น๊ธฐ ์ฌ์ ์์ธกํ ์ ์๋ ์ฑ๋ฅ ๋ฌธ์ ๋ก ์ด์ด์ง๊ธฐ๋ ํจ
- ๊ทธ๋ ๋ค๋ฉด ๊ผญ ํ์ํด์ผ ํ๋ ์์์ ๋ค๋ฃฐ ๋๋ ์ด๋ป๊ฒ ํด์ผํ ๊น?
๐๋ฐฉ๋ฒ
1) try-finally
- ์ ํต์ ์ผ๋ก ์์์ด ์ ๋๋ก ๋ซํ์ ๋ณด์ฅํ๋ ์๋จ
์ฝ๋ 9-1 try-finally - ๋ ์ด์ ์์์ ํ์ํ๋ ์ต์ ์ ๋ฐฉ์ฑ ์ด ์๋
public class BadBufferedReader extends BufferedReader {
public BadBufferedReader(Reader in, int sz) {
super(in, sz);
}
public BadBufferedReader(Reader in) {
super(in);
}
@Override
public String readLine() throws IOException {
throw new CharConversionException();
}
@Override
public void close() throws IOException {
throw new StreamCorruptedException();
}
}
public class TopLine {
static String firstLineOfFile(String path) throws IOException {
BufferedReader br = new BadBufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}
}
public static void main(String[] args) throws IOException {
System.out.println(firstLineOfFile("pom.xml"));
}
}
๊ฒฐ์
- ์๋ฒ์ ๋ฌผ๋ฆฌ์ ์ธ ๋ฌธ์ ๋ฐ์ ์ readLine()๊ณผ close() ๋ฉ์๋์์ ๋ชจ๋ ์์ธ๊ฐ ๋ฐ์ํ ์ ์์
- ์ด๋ ๋ ๋ฒ์งธ ์์ธ๊ฐ ์ฒซ ๋ฒ์งธ ์์ธ๋ฅผ ๋ฎ์ด์์์ ์คํ ์ถ์ ๋ด์ญ์ ์ฒซ ๋ฒ์งธ ์์ธ์ ๊ดํ ์ ๋ณด๋ ๋จ์ง ์๊ฒ ๋์ด ๋๋ฒ๊น
์ด ์ด๋ ค์์ง
- ๋ ๋ฒ์งธ ์์ธ ๋์ ์ฒซ ๋ฒ์งธ ์์ธ๋ฅผ ๊ธฐ๋กํ๋๋ก ์ฝ๋๋ฅผ ์์ ํ ์ ์์ง๋ง, ์ฝ๋๊ฐ ๋๋ฌด ์ง์ ๋ถํด์ง
์ฝ๋ 9-2 ์์์ด ๋ ์ด์์ด๋ฉด 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();
}
}
2) try-with-resources (๊ถ์ฅ)
- ์์์ ์ธ๊ธํ ๋ฌธ์ ๋ค์ ์๋ฐ 7์ try-with-resources ๋์ ๋ชจ๋ ํด๊ฒฐ๋์์
- catch ์ ๋ ํจ๊ป ์ฌ์ฉ ๊ฐ๋ฅํจ
- ์กฐ๊ฑด
- ํด๋น ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ํด๋น ์์์ด AutoCloseable ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํด์ผ ํจ
- ์๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์๋ํํฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ์๋ง์ ํด๋์ค์ ์ธํฐํ์ด์ค๊ฐ ์ด๋ฏธ ํด๋น ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ฑฐ๋ ํ์ฅํด๋
- ๋ซ์์ผ ํ๋ ์์์ ๋ปํ๋ ํด๋์ค๋ฅผ ์์ฑํ๋ค๋ฉด ํด๋น ์ธํฐํ์ด์ค๋ฅผ ๋ฐ๋์ ๊ตฌํํ๋ ๊ฒ์ด ์ข์
- ํด๋น ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ํด๋น ์์์ด AutoCloseable ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํด์ผ ํจ
์ฝ๋ 9-3 try-with-resources - ์์์ ํ์ํ๋ ์ต์ ์ฑ !
public class TopLine {
static String firstLineOfFile(String path) throws IOException {
try(BufferedReader br = new BadBufferedReader(new FileReader(path))) { //try-with-resources ์ ์ฉ
return br.readLine();
}
}
public static void main(String[] args) throws IOException {
System.out.println(firstLineOfFile("pom.xml"));
}
}
๊ฐ์
- readLine()๊ณผ close() ํธ์ถ ๋ชจ๋์์ ์์ธ๊ฐ ๋ฐ์ํ๋ฉด, close()์์ ๋ฐ์ํ ์์ธ๋ ์จ๊ฒจ์ง๊ณ readLine()์์ ๋ฐ์ํ ์์ธ๊ฐ ๊ธฐ๋ก๋จ
- ์ด๋ ๊ฒ ์จ๊ฒจ์ง ์์ธ๋ค์ ๊ทธ๋ฅ ๋ฒ๋ ค์ง์ง ์๊ณ , ์คํ ์ถ์ ๋ด์ญ์ suppressed ๊ผฌ๋ฆฌํ๋ฅผ ๋ฌ๊ณ ์ถ๋ ฅ๋จ
- ์๋ฐ 7์์ Throwable์ ์ถ๊ฐ๋ getSuppressed ๋ฉ์๋๋ฅผ ์ด์ฉํ๋ฉด ํ๋ก๊ทธ๋จ ์ฝ๋์์ ๊ฐ์ ธ์ฌ ์ ์์
์ด๋ป๊ฒ ์ด๋ ๊ฒ ๋์ํ ๊น?
- target ํด๋ ๋ด์ ํด๋์ค ํ์ผ์ ๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ์ (์ธํ ๋ฆฌ์ ์ด์์ ์ปดํ์ผ๋ ๋ฐ์ดํธ์ฝ๋๋ฅผ ๋ณด๊ธฐ ์ฝ๊ฒ ์ ๊ณตํด์ค)
//TopLine.class
public class TopLine {
public TopLine() {
}
static String firstLineOfFile(String path) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
String var2;
try {
var2 = br.readLine();
} catch (Throwable var5) { //1)
try {
br.close();
} catch (Throwable var4) { //2)
var5.addSuppressed(var4);
}
throw var5;
}
br.close();
return var2;
}
public static void main(String[] args) throws IOException {
String path = args[0];
System.out.println(firstLineOfFile(path));
}
}
- 1) readLine()์์์ ์์ธ๋ฅผ catch๋ฌธ์ผ๋ก ์ก์
- 2) catch๋ฌธ ๋ด์์ close() ๋ฉ์๋๋ฅผ ํธ์ถํ๊ณ , ํด๋น ๋ฉ์๋์์์ ์์ธ๋ฅผ catch๋ฌธ์ผ๋ก ์ก์
- ์ด๋ ๋ฐ์ํ ์์ธ๋ Throwable์ addSuppressed()๋ฅผ ํตํด ์ถ๊ฐ๋จ
์ฝ๋ 9-4 ๋ณต์์ ์์์ ์ฒ๋ฆฌํ๋ try-with-resources
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-finally ๋ง๊ณ , try-with-resources๋ฅผ ์ฌ์ฉํ์
- ์ฝ๋๋ ๋ ์งง๊ณ ๋ถ๋ช ํด์ง๊ณ , ๋ง๋ค์ด์ง๋ ์์ธ ์ ๋ณด๋ ํจ์ฌ ์ ์ฉํจ