π κ°λ
β‘ μ: νμΌ I/O, DB μ°κ²°, λ€νΈμν¬ ν΅μ λ± μμΈ‘ κ°λ₯ν μ€ν¨ κ°λ₯μ±μ΄ μ‘΄μ¬νλ κ²½μ°
π λνμ μΈ Checked Exception
IOException β νμΌ μ
μΆλ ₯ μ€ν¨SQLException β DB μμ
μ€ν¨ClassNotFoundException β ν΄λμ€ λ‘λ μ€ν¨InterruptedException β μ€λ λ μΈν°λ½νΈπ μμ μ½λ
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class CheckedExceptionExample {
public static void main(String[] args) {
try {
File file = new File("test.txt");
FileReader fr = new FileReader(file); // FileNotFoundException λ°μ κ°λ₯
} catch (IOException e) { // λ°λμ μ²λ¦¬ν΄μΌ ν¨
System.out.println("νμΌμ μ°Ύμ μ μμ΅λλ€: " + e.getMessage());
}
}
}
π κ°λ
β‘ μ: μλͺ»λ μΈλ±μ€ μ κ·Ό, null μ κ·Ό, μλͺ»λ νλ³ν λ±
π λνμ μΈ Unchecked Exception
NullPointerException β null κ°μ²΄ μ°Έμ‘°ArrayIndexOutOfBoundsException β λ°°μ΄ μΈλ±μ€ μ΄κ³ΌArithmeticException β 0μΌλ‘ λλκΈ°ClassCastException β μλͺ»λ λ€μ΄μΊμ€ν
π μμ μ½λ
public class UncheckedExceptionExample {
public static void main(String[] args) {
String text = null;
System.out.println(text.length()); // NullPointerException λ°μ
}
}
| κ΅¬λΆ | Checked Exception | Unchecked Exception |
|---|---|---|
| μμΈ μ²λ¦¬ | λ°λμ μ²λ¦¬ν΄μΌ ν¨ | μ²λ¦¬ κ°μ μμ |
| λ°μ μμ | μ»΄νμΌ μ κ²μ¬ | λ°νμμ λ°μ |
| μ£Όλ μμΈ | μΈλΆ νκ²½ λ¬Έμ | νλ‘κ·Έλλ¨Έ μ€μ λλ λ‘μ§ λ¬Έμ |
| λν μμ | IOException, SQLException | NullPointerException, ArithmeticException |
πΉ Checked Exceptionμ μ¬μ©ν΄μΌ νλ κ²½μ°
β‘ μ: νμΌμ΄ μμ μλ μμΌλ―λ‘ λ°λμ μμΈ μ²λ¦¬ νμ
πΉ Unchecked Exceptionμ μ¬μ©ν΄μΌ νλ κ²½μ°
β‘ μ: null μ²΄ν¬ λλ½, μλͺ»λ μΈλ±μ€ μ κ·Ό