ITEM 9: try-with-resources

์ž๋ฐ” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋Š” close ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด ์ง์ ‘ ๋‹ซ์•„์ค˜์•ผํ•˜๋Š” ์ž์›๋“ค์ด ๋งŽ๋‹ค.(InputStream, OutputStream, java.sql.Connection ๋“ฑ) ์ž์› ๋‹ซ๊ธฐ๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋†“์น˜๊ธฐ ์‰ฌ์›Œ ์˜ˆ์ธกํ•  ์ˆ˜ ์—†๋Š” ์„ฑ๋Šฅ ๋ฌธ์ œ๋กœ๋„ ์ด์–ด์ง„๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ ์ƒ๋‹น์ˆ˜๊ฐ€ ์•ˆ์ „๋ง์œผ๋กœ finalizer๋ฅผ ํ™œ์šฉํ•˜๊ณ  ์žˆ์ง€๋งŒ, finalizer๋Š” ๋ฏฟ์„๋งŒ ํ•˜์ง€ ๋ชปํ•˜๋‹ค.(item 8)

์ „ํ†ต์ ์œผ๋กœ ์ž์›์ด ์ œ๋Œ€๋กœ ๋‹ซํž˜์„ ๋ณด์žฅํ•˜๋Š” ์ˆ˜๋‹จ์œผ๋กœ try-finally ๊ฐ€ ์‚ฌ์šฉ๋˜์—ˆ๋‹ค.

    static String firstLineOfFile(String path) throws IOException{
        BufferedReader br = new BufferedReader(new FileReader(path));
        try{
            return br.readLine();
        }finally {
            br.close();
        }
    }

๊ธฐ๊ธฐ์— ๋ฌผ๋ฆฌ์ ์ธ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒจ firstLineOfFile ๋ฉ”์„œ๋“œ ์•ˆ์˜ readLine ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๋‹ค๋ฉด readLine() ๋ฉ”์„œ๋“œ๊ฐ€ ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๊ณ , ๊ฐ™์€ ์ด์œ ๋กœ close ๋ฉ”์„œ๋“œ๋„ ์‹คํŒจํ•  ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์—์„œ ๋‘๋ฒˆ์งธ ์˜ˆ์™ธ๊ฐ€ ์ฒซ๋ฒˆ์งธ ์˜ˆ์™ธ๋ฅผ ์‚ผ์ผœ, ์Šคํƒ ์ถ”์  ๋‚ด์—ญ์— ์ฒซ๋ฒˆ์งธ ์˜ˆ์™ธ์— ๋Œ€ํ•œ ์ •๋ณด๋Š” ๋‚จ์ง€ ์•Š๊ฒŒ ๋œ๋‹ค. ๋‘ ๋ฒˆ์งธ ์˜ˆ์™ธ ๋Œ€์‹  ์ฒซ ๋ฒˆ์งธ ์˜ˆ์™ธ๋ฅผ ๋‚จ๊ธธ ์ˆ˜๋Š” ์žˆ์ง€๋งŒ, ์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ์ง€์ €๋ถ„ํ•ด์ ธ์„œ ์‹ค์ œ๋กœ ๊ทธ๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๊ฑฐ์˜ ์—†๋‹ค.

์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋“ค์€ java 7์˜ try-with-resources ๋•์— ๋ชจ๋‘ ํ•ด๊ฒฐ๋˜์—ˆ๋‹ค. try-with-resources ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํ•ด๋‹น ์ž์›์ด AutoCloseable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผํ•˜๋Š”๋ฐ, ์ˆ˜ ๋งŽ์€ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ด๋ฏธ AutoCloseable์„ ๊ตฌํ˜„ํ•˜๊ฑฐ๋‚˜ ํ™•์žฅํ•ด๋‘์—ˆ๋‹ค.

    static String firstLineOfFile(String path) throws IOException{
        // try - with - resources
        try(BufferedReader br = new BufferedReader(new FileReader(path)){
            return br.readLine();
        }
    }

readLine()๊ณผ close() ํ˜ธ์ถœ ์–‘์ชฝ์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด, close์—์„œ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๋Š” ์ˆจ๊ฒจ์ง€๊ณ , readLine์—์„œ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๋งŒ ๊ธฐ๋ก์ด ๋œ๋‹ค. ๋˜ํ•œ, ์ˆจ๊ฒจ์ง„ ์˜ˆ์™ธ๋“ค๋„ ์Šคํƒ์ถ”์  ๋‚ด์—ญ์— suppressed๋ผ๋Š” ๊ผฌ๋ฆฌํ‘œ๋ฅผ ๋‹ฌ๊ณ  ์ถœ๋ ฅ๋˜๋ฉฐ, Throwable์— ์ถ”๊ฐ€๋œ getSuppressed ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ์—์„œ๋„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

    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

try (SomeResource resource = getResource()) {
    use(resource);
} catch(...) {
    ...
}

try์— ์ž์› ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜๋ฉด, try ์ฝ”๋“œ ๋ธ”๋ก์ด ๋๋‚˜๋ฉด ์ž๋™์œผ๋กœ ์ž์›์„ ์ข…๋ฃŒํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์œผ๋กœ ๋”ฐ๋กœ finally ๋ธ”๋ก์ด๋‚˜ ๋ชจ๋“  catch ๋ธ”๋ก์— ์ข…๋ฃŒ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

ํ•˜์ง€๋งŒ, ์ด ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํ•ด๋‹น ์ž์›์ด AutoCloseable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผํ•œ๋‹ค.

public interface AutoCloseable {

    void close() throws Exception;
}

๋˜ํ•œ, try์•ˆ์— ๋ณต์ˆ˜์˜ ์ž์› ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

try(Something1 s1 = new Something1();
    Something2 s2 = new Something2()) {

} catch(...) {
    ...
}
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๋ฅผ ์‚ฌ์šฉํ•ด๋ผ. ์ฝ”๋“œ๋Š” ๋” ์งง๊ณ  ๋ถ„๋ช…ํ•ด์ง€๋ฉฐ, ๋งŒ๋“ค์–ด์ง€๋Š” ์˜ˆ์™ธ์ •๋ณด๋„ ํ›จ์”ฌ ์œ ์šฉํ•˜๋‹ค. ์ •ํ™•ํ•˜๊ณ  ์‰ฝ๊ฒŒ ์ž์›์„ ํšŒ์ˆ˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฐธ๊ณ 

Last updated