[Java] Exception, try-catch-finally, try-with-resources
ํ๋ก๊ทธ๋จ ์คํ ์ค์ ๋ฐ์ํ์ฌ ํ๋ก๊ทธ๋จ ๋ช ๋ น์ ์ ์์ ์ธ ํ๋ฆ์ ๋ฐฉํดํ๋ ์ด๋ฒคํธ
- ๋ฉ์๋ ๋ด์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉด ๋ฉ์๋๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ์ด๋ฅผ ๋ฐํ์ ์์คํ
์ ์ ๋ฌํ๋ค
===> ์์ธ ๊ฐ์ฒด์๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ ๋น์์ ํ๋ก๊ทธ๋จ ์ํ, ์ ํ์ ํฌํจํ ์ค๋ฅ์ ๋ํ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ค

try : exception์ ๋ํ handler(catch)๋ฅผ ์ ๊ณตํด์ผ ํ๋ค
throws : exception์ ๋์ง ์ ์๋ ๋ฉ์๋๋ผ๋ ๊ฒ์ ๋ช
์ํ๋ ๊ตฌ๋ฌธ

1. ์ค๋ฅ ์ฒ๋ฆฌ ์ฝ๋๋ฅผ ์ผ๋ฐ ์ฝ๋์ ๋ถ๋ฆฌํ ์ ์๋ค
2. ํธ์ถ ์คํ ์๋ก ์ค๋ฅ๋ฅผ ์ ํํ ์ ์๋ค
3. ์ค๋ฅ ์ ํ์ ๊ทธ๋ฃนํํ๊ณ ๊ตฌ๋ณํ ์ ์๋ค
์ ์์ฑ๋ ์ ํ๋ฆฌ์ผ์ด์
์ด ์์ํ๊ณ ๋ณต๊ตฌํด์ผ ํ๋ ์์ธ
Error, RuntimeException๊ณผ ๊ทธ ์๋ธ ํด๋์ค๋ฅผ ์ ์ธํ ๋ชจ๋ Exception
ex) java.io.FileNotFoundException
์ ํ๋ฆฌ์ผ์ด์
์ธ๋ถ์ ์๋ ์์ธ์ ์ธ ์กฐ๊ฑด, ์ ํ๋ฆฌ์ผ์ด์
์ด ์์ํ๊ฑฐ๋ ๋ณต๊ตฌํ ์ ์๋ ์์ธ
์ค๋ฅ๋ Catch ๋๋ Specific Requirement(ํ๋ ๋๋ ์ง์ ์๊ตฌ์ฌํญ)์ ์ ์ฉ์ ๋ฐ์ง ์์
Error์ ๊ทธ ํ์ ํด๋์ค
ex) java.io.IOError
์ ํ๋ฆฌ์ผ์ด์
๋ด๋ถ์ ์๋ ์์ธ์ ์ธ ์กฐ๊ฑด, ์ ํ๋ฆฌ์ผ์ด์
์ด ์์ํ๊ฑฐ๋ ๋ณต๊ตฌํ ์ ์์
๋
ผ๋ฆฌ ์ค๋ฅ๋ API์ ๋ถ์ ์ ํ ์ฌ์ฉ๊ณผ ๊ฐ์ ํ๋ก๊ทธ๋๋ฐ ๋ฒ๊ทธ
์์ธ ๋ฐ์์ ์ผ๊ธฐํ ๋ฒ๊ทธ๋ฅผ ์ ๊ฑฐํด์ผ ํจ
ex) NullPointerException
try-with-resources ๋ฌธ์ Closeable๊ณผ ๊ฐ์ ๋ฆฌ์์ค(ex Stream)๋ฅผ ์ฌ์ฉํ๋ ์ํฉ์ ์ ํฉํจ
// Note: This class will not compile yet.
import java.io.*;
import java.util.List;
import java.util.ArrayList;
public class ListOfNumbers {
private List<Integer> list;
private static final int SIZE = 10;
public ListOfNumbers () {
list = new ArrayList<Integer>(SIZE);
for (int i = 0; i < SIZE; i++) {
list.add(new Integer(i));
}
}
public void writeList() {
// The FileWriter constructor throws IOException, which must be caught.
PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++) {
// The get(int) method throws IndexOutOfBoundsException, which must be caught.
out.println("Value at: " + i + " = " + list.get(i));
}
out.close();
}
}
IOException ๊ณผ IndexOutOfBoundsException์ด ์ฒ๋ฆฌ๋์ด์ผ ํ๋ค
1. ์์ธ๋ฅผ ๋ฐ์์ํฌ ์ ์๋ (1)๊ฐ ์ฝ๋ ์ค์ try ๋ธ๋ก ๋ด์ ๋ฐฐ์นํ๊ณ (2) ๊ฐ๊ฐ์ ๋ํด ๋ณ๋์ exception handler๋ฅผ ์ ๊ณตํ๋ค
private List<Integer> list;
private static final int SIZE = 10;
public void writeList() {
PrintWriter out = null;
try { //ํ๋์ try block
System.out.println("Entered try statement");
FileWriter f = new FileWriter("OutFile.txt"); // IOException
out = new PrintWriter(f);
for (int i = 0; i < SIZE; i++) {
out.println("Value at: " + i + " = " + list.get(i)); // IndexOutOfBoundsException
}
}
catch and finally blocks . . .
}
try {
} catch (IndexOutOfBoundsException e) {
System.err.println("IndexOutOfBoundsException: " + e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
}
Java SE 7 ์ด์์์๋ ๋จ์ผ catch ๋ธ๋ก์ด ๋ ๊ฐ์ง ์ด์์ ์์ธ ์ ํ์ ์ฒ๋ฆฌํ ์ ์์
catch (IOException|SQLException ex) {
logger.log(ex);
throw ex;
}
์์ธ๊ฐ ๋ฐ์ํด๋ ๋ธ๋ก์ด ์คํ๋๋ค. ๊ทธ๋์ ์ค์๋ก return, continue ์ ์ํด ์ ๋ฆฌ ์ฝ๋๋ฅผ ์ฐํํ๋ ๊ฒ์ ๋ฐฉ์งํ ์ ์๋ค.
์์ ์์ ์์ 3๊ฐ์ง ๋ฐฉ๋ฒ ์ค ํ๋๋ก finally ๋ธ๋ก์ด ์ข
๋ฃ๋ ์ ์๊ธฐ ๋๋ฌธ์ ๋ค์ ๋ณต์กํ๋ค.
์ด๋ ๊ฒ 3๊ฐ์ง ๋ฐฉ๋ฒ ์ค ํ๋๋ก ์คํ๋ ์ ์๊ธฐ ๋๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ ์ฝ๋๊ฐ ์์ฑ๋์ด์ผ ํ๋ค
finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
} else {
System.out.println("PrintWriter not open");
}
if (f != null) {
System.out.println("Closing FileWriter");
f.close();
}
}
try-with-resources ๊ตฌ๋ฌธ์ ๋ ์ด์ ํ์ํ์ง ์์ ์์คํ ๋ฆฌ์์ค๋ฅผ ์๋์ผ๋ก releaseํ๋ค
public void writeList() throws IOException {
try (FileWriter f = new FileWriter("OutFile.txt");
PrintWriter out = new PrintWriter(f)) {
for (int i = 0; i < SIZE; i++) {
out.println("Value at: " + i + " = " + list.get(i));
}
}
}
๊ตฌ๋ฌธ์ ๋์์ resource๊ฐ ๋ซํ๋ ๊ฒ์ด ๋ณด์ฅ๋๋ค
java.lang.AutoCloseable์ด๋ java.io.Closeable์ ๊ตฌํํ๋ Object๋ resource๋ก ์ฌ์ฉ๋ ์ ์๋ค
Java SE 7์ดํ์๋ FileReader, BufferedReader ๊ฐ java.lang.AutoCloseable์ ๊ตฌํํ๋ค.
๊ทธ๋ฌ๋ฏ๋ก, try-with-resources์ ์ฌ์ฉํ ์ ์๊ณ , try๋ฌธ์ด ์ ์์ ์ผ๋ก ์๋ฃ๋์๋์ง ์ฌ๋ถ์ ๊ด๊ณ์์ด resources๋ ๋ซํ๊ฒ ๋๋ค.
static String readFirstLineFromFile(String path) throws IOException {
try (FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr)) {
return br.readLine();
}
}
Java SE 7 ์ด์ ์๋ finally ๋ธ๋ก์์ ๋ฆฌ์์ค๋ฅผ ๋ซ๋๋ก ์ฒ๋ฆฌํ๋ค. ํ์ง๋ง, ๋ฆฌ์์ค ๋์๊ฐ ์์ ์ ์๋ค. ๋ฆฌ์์ค์ close ๋ฉ์๋๋ฅผ ํธ์ถํด์ ์ด์์ฒด์ ์๊ฒ ํด์ ๋ฅผ ์์ฒญํด์ผ ํ๋ค. ํ์ง๋ง, GC๊ฐ ๋ฆฌ์์ค๋ฅผ ํ์ํ๊ธฐ ์ ์ ์ด ์์
์ ์ํํ์ง ๋ชปํ๋ฉด ๋ฆฌ์์ค๋ฅผ ํด์ ํ๋ ๋ฐ ํ์ํ ์ ๋ณด๊ฐ ์์ค๋๋ค.
- ๋ฆฌ์์ค์ ๋ฉ์๋๋ ์์ฑ ์์์ ๋ฐ๋ ์์๋ก ํธ์ถ๋๋ค.
public static void writeToFileZipFileContents(String zipFileName,
String outputFileName)
throws java.io.IOException {
java.nio.charset.Charset charset =
java.nio.charset.StandardCharsets.US_ASCII;
java.nio.file.Path outputFilePath =
java.nio.file.Paths.get(outputFileName);
// Open zip file and create output file with
// try-with-resources statement
try (
java.util.zip.ZipFile zf =
new java.util.zip.ZipFile(zipFileName);
java.io.BufferedWriter writer =
java.nio.file.Files.newBufferedWriter(outputFilePath, charset)
) {
// Enumerate each entry
for (java.util.Enumeration entries =
zf.entries(); entries.hasMoreElements();) {
// Get the entry name and write it to the output file
String newLine = System.getProperty("line.separator");
String zipEntryName =
((java.util.zip.ZipEntry)entries.nextElement()).getName() +
newLine;
writer.write(zipEntryName, 0, zipEntryName.length());
}
}
}
https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html
Lesson: Exceptions (The Java™ Tutorials > Essential Java Classes)
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated
docs.oracle.com
| [์๋ฐ] ์ถ์ ํด๋์ค์ ์ธํฐํ์ด์ค์ ๋ํด ์ค๋ช ํด์ฃผ์ธ์ (0) | 2025.03.27 |
|---|---|
| [JAVA] Random vs SecureRandom vs ThreadLocalRandom (0) | 2024.12.26 |
| [์๋ฐ] Collections API : TreeMap (0) | 2024.10.22 |
| [Java] Reflection (0) | 2024.01.06 |
| [์๋ฐ] ์ดํฉํฐ๋ธ์๋ฐ : 2. ์์ฑ์์ ๋งค๊ฐ๋ณ์๊ฐ ๋ง๋ค๋ฉด ๋น๋๋ฅผ ๊ณ ๋ คํด๋ผ (0) | 2023.10.17 |