728x90
๐ ์๋ก
try(Scanner sc = new Scanner(new File(fileName), encoding)) {...
์ต์ํ ๋ฏ ์ต์ํ์ง ์์ ์์ ๊ฐ์ ๋ฌธ์ฅ์ ๋ณด๋ฉฐ... ๋ด๊ฐ ๋ณด๊ณ ์๋ ์ฝ๋๊ฐ ์๋ฐ๊ฐ ๋ง๋๊ฐ? ๋ผ๋ฉฐ, ์ฐพ์๋ณด๋ฉด์ java 7๋ถํฐ ์์ธ ์ฒ๋ฆฌ ๋ฌธ๋ฒ์ด ๋ณด์๋์์์ ์๊ฒ ๋์๋ค. ํฌ๊ฒ ์ด๋ ค์์ง ์ ์ ์์ผ๋ ์์๋ณด๋๋ก ํ์.
๐ ๋ณธ๋ก
ํฌ๊ฒ ๋ ๋ถ๋ถ์์ ๋ณํ๊ฐ ์์๋๋ฐ ์ฐ์ try, catch, and finally ๋ถ๋ถ์ ์์ด์์ ๋ณด์๋ ์ ๊ณผ try-with-resources ๋ฌธ์ด ์๋ฐ 7๋ถํฐ ๋์ ๋ ๋ถ๋ถ์ด๋ค.
๐ฏ try, catch, and finally
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package com.study;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class Java7ExceptionSample {
private List<Integer> list;
private static final int SIZE = 10;
public static void main(String[] args) {
Java7ExceptionSample sample = new Java7ExceptionSample();
sample.writeList();
}
public Java7ExceptionSample () {
list = new ArrayList<Integer>(SIZE);
for (int i = 0; i < SIZE; i++) {
list.add(i);
}
}
public void writeList() {
// FileWirter ์์ฑ์๋ IOException์ ๋ฐ์ ์ํค๊ธฐ ๋๋ฌธ์ ๋ฐ๋์ ์ด๋ฅผ ์ก์์ฃผ๋ ๊ตฌ๋ฌธ์ด ํ์ํ๋ค.
PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++) {
// get(int) ๋ฉ์๋๋ IndexOutOfBoundsException ์์ธ๋ฅผ ๋ฐ์ ์ํค๊ธฐ ๋๋ฌธ์ ๋ฐ๋์ ์ด๋ฅผ ์ก์์ฃผ๋ ๊ตฌ๋ฌธ์ด ํ์ํ๋ค.
out.println("Value at: " + i + " = " + list.get(i));
}
out.close();
}
}
|
cs |
์ฐ์ ์์ ์์ ๋ฅผ IDE์ ๋ฃ์ด๋ณด๋ฉด, writeList() ๋ฉ์๋์ FileWriter๋ถ๋ถ์ ์์ธ์ฒ๋ฆฌ๋ฅผ ํด์ค์ผ ๋ ๋ค๋ ๋นจ๊ฐ ๋ฐ์ค์ ๋ง๋๊ฒ ๋ ๊ฒ์ด๋ค. ๋ ๊ทธ ์๋์ค์ ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ฅผ ์ฒ๋ฆฌํ๋ ๋ถ๋ถ์์ ์ธ๋ฑ์ค๋ฅผ ๋ฒ์ด๋ ๋ฆฌ์คํฌ๊ฐ ์กด์ฌํ๊ธฐ ๋๋ฌธ์ ๊ทธ ๋ถ๋ถ๋ ์์ธ์ฒ๋ฆฌ ๊ตฌ๋ฌธ์ผ๋ก ์ฒ๋ฆฌํด์ค ์ ์์ ๊ฒ์ด๋ค.
์ฆ writeList() ๋ฉ์๋๋ IOException๊ณผ IndexOutOfBoundsException ์ด๋ฐ ์์ธ๊ฐ ๋ฐ์ํ ๊ฐ๋ฅ์ฑ์ด ์๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public void writeList() {
// FileWirter ์์ฑ์๋ IOException์ ๋ฐ์ ์ํค๊ธฐ ๋๋ฌธ์ ๋ฐ๋์ ์ด๋ฅผ ์ก์์ฃผ๋ ๊ตฌ๋ฌธ์ด ํ์ํ๋ค.
PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++) {
// get(int) ๋ฉ์๋๋ IndexOutOfBoundsException ์์ธ๋ฅผ ๋ฐ์ ์ํค๊ธฐ ๋๋ฌธ์ ๋ฐ๋์ ์ด๋ฅผ ์ก์์ฃผ๋ ๊ตฌ๋ฌธ์ด ํ์ํ๋ค.
out.println("Value at: " + i + " = " + list.get(i));
}
} catch (IOException e) {
e.printStackTrace();
} catch (IndexOutOfBoundsException i) {
i.printStackTrace();
} finally {
out.close();
}
}
|
cs |
writeList() ๋ฉ์๋ ๋ถ๋ถ์์ ์์ธ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ๋ฉด ์์ ๊ฐ์ด ํด์ค์ผ ํ ๊ฒ์ด๋ค. Catch ๋ถ๋ถ์ ๋ณด๋ฉด ๋ ์ค๋ก ์ฒ๋ฆฌ๋์์์ ์ ์ ์๋ค. ๋ฌผ๋ก Exception e ์ด๋ฐ ์์ผ๋ก ํ ๋ฒ์ ํด์ค ์๋ ์์ ๊ฒ์ด์ง๋ง, ๊ทธ๋ ๊ฒ ํ์ง ์๋๋ค๊ณ ๊ฐ์ฅํ๋ฉด ์ ๋ ๊ฒ ์์ธ ์ข ๋ฅ์ ๋ฐ๋ผ catch๋ฌธ์ด ์ ์ ๋์ด๋๊ฒ ๋ ๊ฒ์ด๋ค.
1
2
3
|
catch (IOException | IndexOutOfBoundsException ex) {
ex.printStackTrace();
}
|
cs |
!!!! ์์ catch๋ฌธ์์ | ๋ฅผ ํตํด ์ด์ด์ ํ ๋ฒ์ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํด์ก๋ค.
๐ฏ try-with-resources
์ด๋ ํ ์คํธ๋ฆผ ๊ฐ์ฒด๋ฅผ ์ฐ๊ณ ๋๋ฉด ๋ฐ๋์ ์์์ ํด์ ์์ผ์ค์ผ ํ๋ค. ์๋์ ๊ฐ์ ์์ผ๋ก ๋ง์ด๋ค.
์ํฉ์ ๋ฐ๋ผ์๋ ์ ๋ถ๋ถ๋ ์ฌ์ฉํ ์คํธ๋ฆผ ๊ฐ์ฒด๋งํผ ๋ซ์์ผ ํ๊ธฐ ๋๋ฌธ์ ์์์ ๋ดค๋ Java 7 ๋ฒ์ ์ด์ ์ catch๋ฌธ์ฒ๋ผ ์ง์ ๋ถํด์ง ์ ์๊ฒ ๋๋ค.
1
2
3
4
5
|
finally {
if(out != null) {
out.close();
}
}
|
cs |
์ด๋ฌํ ๋ถ๋ถ์ ์์ด Java 7 ๋ฒ์ ์์๋ try-with-resources ๋ฌธ์ ๋์ ํ์๋ค. ์ฝ๋๋ฅผ ๋จผ์ ๋ณด์.
1
2
3
4
5
6
7
8
9
10
|
public void writeList2() {
// try-with-resources ๋ฌธ
try( PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt")) ) {
for (int i = 0; i < SIZE; i++) {
out.println("Value at: " + i + " = " + list.get(i));
}
} catch (IOException | IndexOutOfBoundsException ex) {
ex.printStackTrace();
}
}
|
cs |
๋ชจ์์ try(๋ฆฌ์์ค๋ค..) ์ด๋ ๊ฒ ๋ฃ์ด์ฃผ๋ฉฐ ์ด์ด๊ฐ์ฃผ๋ฉด ๋๋ค. ์ฌ๊ธฐ์ ๋ฆฌ์์ค๋ฅผ ์๋ฏธํ๋ ๊ฒ์ ํ๋ก๊ทธ๋จ์ด ๋๋ ํ ๋ซ์์ผ ํ๋ ๊ฐ์ฒด๋ฅผ ์๋ฏธํ๋ค. ๋ฆฌ์์ค ๋ฌธ์ ๊ฐ ๋ฆฌ์์ค๊ฐ ์ฌ์ฉ์ ์๋ฃํ ๋ค ์๋์ ๋ซํ๋๋ก ํ๋ค.
Java 5๋ถํฐ ์ถ๊ฐ๋ java.io.Closeable์ด๋ผ๋ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค๋ค์ ๋ช ์์ ์ผ๋ก close() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ซ์์ฃผ์ด์ผ๋ง ํ๋ค.
ํ์ง๋ง Java 7๋ถํฐ ํด๋น ์ธํฐํ์ด์ค๋ "public interface Closeable extends AutoCloseable" ์ด๋ ๊ฒ ์ ์ธ ๋์๋ค. ์ฌ๊ธฐ์ AutoCloseable์ด๋ผ๋ ์ธํฐํ์ด์ค๋ฅผ ์์๋ฐ์ ๊ฒ์ ๋ณผ ์ ์๋ค.
๋ ์ด ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค๋ try-with-resources ๋ฌธ์ฅ์์ ์ฌ์ฉํ ์ ์๋ค. ๋งํฌ๋ฅผ ๋๋ฅด๋ฉด ๋ํ๋จผํธ๋ฅผ ํตํด ํ์ธํ ์ ์๋ค.
๐ ๊ฒฐ๋ก
๋ํ๋จผํธ๋ฅผ ์๊พธ ๋ณด๋ ค๊ณ ๋ ธ๋ ฅ ์ค์ธ๋ฐ ์ ๋ค๋ฆญ ๋๋ ๊ฐ๋ ์์ฒด๋ ์ด๋ ค์ ์ด์ ์ฝ์ง ์์์ง๋ง, ์์ธ ๋ฌธ์ ์ด๋ ต์ง ์์ ๋ํ๋จผํธ๊ฐ ์ฝ๊ฒ ์ฝํ ์์ธ ๋ฌธ์ ๊ธฐ๋ฐ์ผ๋ก ์์ฑํ ์ ์์๋ค. ๋์ฑ ๋ํ๋จผํธ์ ์นํด์ง๋ ค๊ณ ๋ ธ๋ ฅํด์ผ๊ฒ ๋ค.
์ฐธ๊ณ ์๋ฃ
- ์๋ฐ์์ (์ด์๋ฏผ ์ )
- ๊ณต์๋ฌธ์
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
๋ฐ์ํ
'SKILL > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์๋ฐ : ์ค๋ ๋(2) (0) | 2021.09.08 |
---|---|
์๋ฐ : ์ค๋ ๋(1) (0) | 2021.09.07 |
์๋ฐ : Heap pollution ์ด๋? (0) | 2021.09.02 |
์ ๋ค๋ฆญ์ด๋? (0) | 2021.09.01 |
์คํ๋ง ๋ถํธ์ aws๋ก ํผ์ ๊ตฌํํ๋ ์น์๋น์ค : ๋ก๊ทธ์ธ ๊ธฐ๋ฅ ๊ตฌํ oauth (0) | 2021.07.18 |