๐ฃ์ํ์ฝ๋ฉ ๊ฐ์๐ฃ
Java ์์ธ - https://www.youtube.com/playlist?list=PLuHgQVnccGMCrFJLxpjhE0N5tvOVxJuVB
๋ฌธ์ ์ํฉ ํ์ ํ๊ธฐ
public class Main {
public static void main(String[] args) {
System.out.println(1);
System.out.println(2/0); // ๋ฌธ์ ์ ์ฝ๋
System.out.println(3);
}
}
โก๏ธ ์์ธ ์ฒ๋ฆฌ๊ฐ ํ์ํ๋ค
Try - Catch ์ ์ฉํ๊ธฐ
public class Main {
public static void main(String[] args) {
System.out.println(1);
int[] scores = {10, 20, 30};
try { // exception
System.out.println(scores[3]);
System.out.println(2 / 0);
} catch(ArithmeticException e) { // 2/0 ์์ธ ์ฒ๋ฆฌ
System.out.println("์๋ชป๋ ๊ณ์ฐ์
๋๋ค"+e.getMessage());
e.printStackTrace();
} catch(ArrayIndexOutOfBoundsException e) { // scores[3] ์์ธ ์ฒ๋ฆฌ
System.out.println("๋ฒ์๋ฅผ ๋ฒ์ด๋ฌ์ต๋๋ค"+e.getMessage());
e.printStackTrace();
}
System.out.println(3);
}
}
โ๏ธ e.getMessage() : ์ด๋ค ์๋ฌ์ธ์ง ํ์ธํ๊ธฐโ๏ธ e.printStackTrace() : ์๋ฌ ๋ฉ์ธ์ง ์ถ๋ ฅํ๊ธฐ
โ๏ธ ๋ถ๋ชจ Exception ์ด ์์ Exception (ex. ArithmeticException / ArrayIndexOutOfBoundsException) ์ ํฌํจํ์ฌ ์์ธ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ๋ค → catch(Exception e) ๋ก ๋์ฒด ๊ฐ๋ฅ
โ๏ธ ๋ณ๊ฒฝ๋ ์ฝ๋
public class Main {
public static void main(String[] args) {
...
try { // exception
System.out.println(scores[3]);
System.out.println(2 / 0);
} catch(Exception e) { // ๋ณ๊ฒฝ
System.out.println("์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค");
}
...
}
}
Try - Catch - Finally ์ ์ฉํ๊ธฐ
· Exception : Checked Exception & Unchecked Exception
· Checked Exception : ์์ธ ์ฒ๋ฆฌ๊ฐ ํ์
· RunTimeException ์ ์ ์ธํ ๋๋จธ์ง Exception (ex. IOException) ์ ๋ชจ๋ Checked Exception
· IOException : FileNotFoundException
โ๏ธ ํ๋ก์ ํธ ๋ด ํ์ผ ์์ฑํ๋ ์ฝ๋
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
FileWriter f = null;
try { // ํ์์ ์ผ๋ก ์์ธ ์ฒ๋ฆฌ ํ์
f = new FileWriter("data.txt");
f.write("Hello Exception!");
// close ํ๊ธฐ ์ ์์ธ๊ฐ ๋ฐ์ํ ์ ์๊ธฐ ๋๋ฌธ์ finally ๋ก ์ฒ๋ฆฌํด์ผ ํ๋ค
// f.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
// ํ์ผ ์กด์ฌ ์ฌ๋ถ ํ์
ํ๊ธฐ
if (f != null) {
try {
f.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Try with Resource Statements
โ๏ธ ์์ Try - Catch - Finally ์ ๊ฐ์ ๋์์ ์ํํ๋ ์ฝ๋
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try (FileWriter f = new FileWriter("data.txt") {
f.write("Hello Exception!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
→ f.close() ๊ฐ ์ฌ๋ผ์ก๋ค
ThrowException
· ์์ธ ์์ฑํ๊ธฐ : throws
· Java7 ๋ถํฐ ์ฌ์ฉ ๊ฐ๋ฅ
โ๏ธ ์์ Try - Catch - Finally / Try with Resource Statements ์ ๊ฐ์ ๋์์ ์ํํ๋ ์ฝ๋
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
FileWriter f = new FileWriter("./data.txt");
}
}
'Spring > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Java ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ (2) | 2023.09.29 |
---|---|
Java Method (0) | 2023.09.28 |
์ฑ ๋ง๋ค๊ธฐ 2 - ๋ฐฐ๋น๊ธ ๊ณ์ฐ๊ธฐ (์ ์ด๋ฌธ, ๋ฐฐ์ด, ๋ฉ์๋, ํด๋์ค, ์ธ์คํด์ค) (0) | 2023.09.25 |
์ฑ ๋ง๋ค๊ธฐ 1 - ๋ฐฐ๋น๊ธ ๊ณ์ฐ๊ธฐ (๋ณ์, ์ ๋ ฅ๊ฐ) (0) | 2023.09.22 |
๋ณ์ (0) | 2023.09.21 |