** πμμΈμ²λ¦¬ **
π’ 1οΈβ£ μμΈ(Exception)λ?
π‘ μμΈ(Exception)λ νλ‘κ·Έλ¨ μ€ν μ€μ λ°μνλ μμμΉ λͺ»ν μ€λ₯μΌ!
μλ₯Ό λ€μ΄, λ€μκ³Ό κ°μ κ²½μ° νλ‘κ·Έλ¨μ΄ λ©μΆ μ μμ΄.
β 0μΌλ‘ λλκΈ° (ArithmeticException)
int result = 10 / 0; // μμΈ λ°μ!
β λ°°μ΄ μΈλ±μ€ μ΄κ³Ό (ArrayIndexOutOfBoundsException)
int[] arr = {1, 2, 3};
System.out.println(arr[5]); // μμΈ λ°μ!
β λ¬Έμλ₯Ό μ«μλ‘ λ³ν μ μ€λ₯ (NumberFormatException)
int num = Integer.parseInt("abc"); // μμΈ λ°μ!
β
null
κ°μ μ°Έμ‘° (NullPointerException)
String str = null;
System.out.println(str.length()); // μμΈ λ°μ!
π μ΄λ° μ€λ₯(μμΈ)λ₯Ό μ²λ¦¬νμ§ μμΌλ©΄ νλ‘κ·Έλ¨μ΄ λ©μΆ°λ²λ €!
π κ·Έλμ βμμΈ μ²λ¦¬βλ₯Ό ν΅ν΄ νλ‘κ·Έλ¨μ΄ λ©μΆμ§ μκ³ κ³μ μ€νλλλ‘ ν΄μΌ ν΄.
π’ 2οΈβ£ μμΈ μ²λ¦¬ λ°©λ² (try-catch)
π‘ μμΈ μ²λ¦¬λ βλ¬Έμ κ° λ°μν΄λ νλ‘κ·Έλ¨μ΄ λ©μΆμ§ μλλ‘ νλ κΈ°μ βμ΄μΌ.
κ°μ₯ κΈ°λ³Έμ μΈ λ°©λ²μ΄ try-catch
λ¬Έμ΄μΌ!
β
try-catch
κΈ°λ³Έ μ¬μ©λ²
try {
int num = 10 / 0; // μμΈ λ°μ!
} catch (ArithmeticException e) {
System.out.println("μμΈ λ°μ! 0μΌλ‘ λλ μ μμ΅λλ€.");
}
System.out.println("νλ‘κ·Έλ¨μ΄ μ μ μ€νλ©λλ€.");
π μΆλ ₯ κ²°κ³Ό
μμΈ λ°μ! 0μΌλ‘ λλ μ μμ΅λλ€.
νλ‘κ·Έλ¨μ΄ μ μ μ€νλ©λλ€.
β μμΈκ° λ°μν΄λ νλ‘κ·Έλ¨μ΄ λ©μΆμ§ μκ³ μ μμ μΌλ‘ μ€νλΌ!
π’ 3οΈβ£ finally λΈλ‘: μμΈ λ°μ μ¬λΆμ μκ΄μμ΄ μ€ν
π‘ finally
λΈλ‘μ μμΈκ° λ°μνλ μ νλ 무쑰건 μ€νλΌ!
μ΄κ±Έ μ£Όλ‘ νμΌ λ«κΈ°, DB μ°κ²° ν΄μ , μμ μ 리ν λ μ¬μ©ν΄.
β
finally
κΈ°λ³Έ μμ
try {
System.out.println("try λΈλ‘ μ€ν");
int result = 10 / 2; // μμΈ μμ
System.out.println("κ²°κ³Ό: " + result);
} catch (ArithmeticException e) {
System.out.println("μμΈ λ°μ!");
} finally {
System.out.println("finally λΈλ‘ μ€ν!");
}
System.out.println("νλ‘κ·Έλ¨ μ’
λ£");
π μΆλ ₯ κ²°κ³Ό
try λΈλ‘ μ€ν
κ²°κ³Ό: 5
finally λΈλ‘ μ€ν!
νλ‘κ·Έλ¨ μ’
λ£
β
finally
λΈλ‘μ μμΈ λ°μ μ¬λΆμ κ΄κ³μμ΄ νμ μ€νλ¨!
π’ 4οΈβ£ μ¬λ¬ κ°μ catch λΈλ‘ μ¬μ©νκΈ°
π‘ μ¬λ¬ μ’ λ₯μ μμΈκ° λ°μν μ μμ λ, κ°κ° λ€λ₯΄κ² μ²λ¦¬ν μ μμ΄!
β
try-catch
μμ μ¬λ¬ κ°μ μμΈ μ²λ¦¬
try {
int[] arr = {1, 2, 3};
System.out.println(arr[5]); // λ°°μ΄ λ²μλ₯Ό λ²μ΄λ μ κ·Ό (μμΈ λ°μ!)
} catch (ArithmeticException e) {
System.out.println("μ°μ μ€λ₯ λ°μ!");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("λ°°μ΄ μΈλ±μ€ μ€λ₯ λ°μ!");
} catch (Exception e) {
System.out.println("μ μ μλ μ€λ₯ λ°μ!");
}
π μΆλ ₯ κ²°κ³Ό
λ°°μ΄ μΈλ±μ€ μ€λ₯ λ°μ!
β
λ°μν μμΈμ λ§λ catch
λΈλ‘μ΄ μ€νλ¨!
π’ 5οΈβ£ throw ν€μλ: μ§μ μμΈ λ°μμν€κΈ°
π‘ throw
λ₯Ό μ¬μ©νλ©΄ κ°λ°μκ° μ§μ μμΈλ₯Ό λ°μμν¬ μ μμ΄!
β
throw
κΈ°λ³Έ μ¬μ©λ²
public class ThrowExample {
public static void checkAge(int age) {
if (age < 18) {
throw new IllegalArgumentException("18μΈ μ΄μλ§ μ΄μ© κ°λ₯ν©λλ€!");
}
System.out.println("μ΄μ© κ°λ₯ν©λλ€.");
}
public static void main(String[] args) {
checkAge(16); // μμΈ λ°μ
}
}
π μΆλ ₯ κ²°κ³Ό
Exception in thread "main" java.lang.IllegalArgumentException: 18μΈ μ΄μλ§ μ΄μ© κ°λ₯ν©λλ€!
β
throw
λ₯Ό μ¬μ©νλ©΄ νΉμ 쑰건μμ κ°μ λ‘ μμΈλ₯Ό λ°μμν¬ μ μμ΄!
π’ 6οΈβ£ throws ν€μλ: μμΈλ₯Ό νΈμΆν κ³³μΌλ‘ λκΈ°κΈ°
π‘ λ©μλμμ μμΈλ₯Ό μ§μ μ²λ¦¬νμ§ μκ³ , νΈμΆν κ³³μμ μ²λ¦¬νλλ‘ λκΈΈ λ μ¬μ©ν΄!
β
throws
κΈ°λ³Έ μ¬μ©λ²
public class ThrowsExample {
public static void divide(int a, int b) throws ArithmeticException {
if (b == 0) {
throw new ArithmeticException("0μΌλ‘ λλ μ μμ΅λλ€.");
}
System.out.println("κ²°κ³Ό: " + (a / b));
}
public static void main(String[] args) {
try {
divide(10, 0); // μμΈ λ°μ
} catch (ArithmeticException e) {
System.out.println("μμΈ λ°μ: " + e.getMessage());
}
}
}
π μΆλ ₯ κ²°κ³Ό
μμΈ λ°μ: 0μΌλ‘ λλ μ μμ΅λλ€.
β
throws
λ μμΈλ₯Ό μ§μ μ²λ¦¬νμ§ μκ³ νΈμΆν κ³³μμ μ²λ¦¬νλλ‘ λ겨!
π’ 7οΈβ£ μ¬μ©μ μ μ μμΈ λ§λ€κΈ°
π‘ κΈ°λ³Έ μμΈ ν΄λμ€κ° μλ, μ§μ λλ§μ μμΈ ν΄λμ€λ₯Ό λ§λ€ μλ μμ΄!
β μ¬μ©μ μ μ μμΈ μμ
class MyException extends Exception { // μ¬μ©μ μ μ μμΈ ν΄λμ€
public MyException(String message) {
super(message);
}
}
public class CustomExceptionExample {
public static void checkScore(int score) throws MyException {
if (score < 0 || score > 100) {
throw new MyException("μ μλ 0~100 μ¬μ΄μ¬μΌ ν©λλ€!");
}
System.out.println("μ μ μ
λ ₯ μλ£: " + score);
}
public static void main(String[] args) {
try {
checkScore(150); // μμΈ λ°μ
} catch (MyException e) {
System.out.println("μ¬μ©μ μ μ μμΈ λ°μ: " + e.getMessage());
}
}
}
π μΆλ ₯ κ²°κ³Ό
μ¬μ©μ μ μ μμΈ λ°μ: μ μλ 0~100 μ¬μ΄μ¬μΌ ν©λλ€!
β μ¬μ©μ μ μ μμΈλ₯Ό λ§λ€λ©΄, νΉμ ν μν©μμ λ μλ―Έ μλ μμΈ λ©μμ§λ₯Ό μ 곡ν μ μμ΄!
π― μ΅μ’ μ 리
μμΈ μ²λ¦¬ λ°©λ² | μ€λͺ |
---|---|
try-catch |
μμΈκ° λ°μν κ°λ₯μ±μ΄ μλ μ½λ μ€ν ν μμΈ μ²λ¦¬ |
finally |
μμΈ λ°μ μ¬λΆμ κ΄κ³μμ΄ λ¬΄μ‘°κ±΄ μ€ν |
throw |
μ§μ μμΈ λ°μ |
throws |
μμΈλ₯Ό νΈμΆν κ³³μΌλ‘ λκΉ |
μ¬μ©μ μ μ μμΈ |
μ§μ μμΈ ν΄λμ€λ₯Ό λ§λ€μ΄ μ¬μ© |
β μ΄μ μμΈ μ²λ¦¬ κ°λ μ μλ²½νκ² μ΄ν΄νμ΄! π
β κΈ°μ λ©΄μ μμ μμ£Ό λμ€λ μ§λ¬Έλ μ 리νμΌλκΉ ν λ² λ 볡μ΅ν΄ 보면 μ’μ! π―
π’ 8οΈβ£ μμΈ κ³μΈ΅ ꡬ쑰 μλ²½ μ΄ν΄
π‘ μλ°μ μμΈ(Exception)λ€μ λΆλͺ¨-μμ κ΄κ³λ‘ μ΄λ£¨μ΄μ§ κ³μΈ΅ ꡬ쑰λ₯Ό κ°μ§κ³ μμ΄.
β μμΈ κ³μΈ΅ ꡬ쑰 (μμ κ΄κ³)
Throwable (λͺ¨λ μμΈμ μ€λ₯μ μ΅μμ ν΄λμ€)
βββ Error (νλ‘κ·Έλ¨μ΄ 볡ꡬν μ μλ μΉλͺ
μ μ€λ₯)
β βββ OutOfMemoryError (λ©λͺ¨λ¦¬ λΆμ‘±)
β βββ StackOverflowError (무ν μ¬κ· νΈμΆ)
β βββ κΈ°ν μμ€ν
μ€λ₯
β
βββ Exception (μΌλ°μ μΈ μμΈ)
βββ RuntimeException (μμΈ μ²λ¦¬λ₯Ό κ°μ νμ§ μμ)
β βββ ArithmeticException (0μΌλ‘ λλκΈ°)
β βββ NullPointerException (null κ° μ κ·Ό)
β βββ NumberFormatException (λ¬Έμμ΄ β μ«μ λ³ν μ€λ₯)
β βββ ArrayIndexOutOfBoundsException (λ°°μ΄ λ²μ μ΄κ³Ό)
β βββ κΈ°ν λ
Όλ¦¬μ μμΈ
β
βββ IOException, SQLException (λ°λμ μμΈ μ²λ¦¬λ₯Ό ν΄μΌ νλ Checked Exception)
β RuntimeExceptionμ μμλ°μΌλ©΄ μμΈ μ²λ¦¬κ° κ°μ λμ§ μμ!
β
Checked Exceptionμ μμλ°μΌλ©΄ λ°λμ try-catch
λλ throws
λ₯Ό μ¬μ©ν΄μΌ ν΄.
π’ 9οΈβ£ Checked Exception vs Unchecked Exception (μ¬ν)
π‘ μμΈλ Checked Exceptionκ³Ό Unchecked Exception λ κ°μ§λ‘ λλμ΄!
β 1. Checked Exception (μμΈ μ²λ¦¬κ° νμ)
Exception
ν΄λμ€λ₯Ό μμλ°μ μμΈ (IOException
,SQLException
λ±)- μ»΄νμΌλ¬κ° μμΈ μ²λ¦¬λ₯Ό κ°μ νκΈ° λλ¬Έμ,
try-catch
λλthrows
λ₯Ό λ°λμ μ¬μ©ν΄μΌ ν΄.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CheckedExceptionExample {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(new File("file.txt")); // νμΌμ΄ μμΌλ©΄ μμΈ λ°μ
} catch (FileNotFoundException e) { // Checked Exception μ²λ¦¬
System.out.println("νμΌμ μ°Ύμ μ μμ΅λλ€.");
}
}
}
π νμΌμ΄ μ‘΄μ¬νμ§ μμΌλ©΄ FileNotFoundException
λ°μ!
π Checked Exceptionμ΄λ―λ‘ λ°λμ try-catch
λ‘ μμΈλ₯Ό μ²λ¦¬ν΄μΌ ν΄.
β 2. Unchecked Exception (μμΈ μ²λ¦¬κ° μ ν)
RuntimeException
μ μμλ°μ μμΈ (NullPointerException
,ArithmeticException
λ±)- μ»΄νμΌλ¬κ° μμΈ μ²λ¦¬λ₯Ό κ°μ νμ§ μμ, μ€ν μ€ μ€λ₯ λ°μ μ νλ‘κ·Έλ¨μ΄ λ©μΆ μ μμ.
public class UncheckedExceptionExample {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
System.out.println(arr[5]); // ArrayIndexOutOfBoundsException λ°μ
}
}
π try-catch
κ° μμ΄λ μ€νμ κ°λ₯νμ§λ§, μ€λ₯ λ°μ μ νλ‘κ·Έλ¨μ΄ λ©μΆ°!
π Unchecked Exceptionμ λ°λμ μμΈ μ²λ¦¬κ° νμν κ²μ μλμ§λ§, νμν κ²½μ° μμΈλ₯Ό μ‘μμΌ ν΄.
π’ π μμΈ μ²λ¦¬ μ±λ₯ κ³ λ €νκΈ°
π‘ μμΈ μ²λ¦¬λ νλ‘κ·Έλ¨μ μμ μ±μ λμ¬μ£Όμ§λ§, μλͺ» μ¬μ©νλ©΄ μ±λ₯μ μ νμν¬ μλ μμ΄!
β 1. μμΈλ₯Ό λ¨μ©νλ©΄ μ λλ μ΄μ
- μμΈ μ²λ¦¬λ λΉμ©μ΄ ν¬κΈ° λλ¬Έμ, λΆνμν μμΈ λ°μμ νΌν΄μΌ ν΄!
- μμΈκ° λ°μν κ°λ₯μ±μ΄ λμ κ²½μ°, 쑰건문μΌλ‘ λ¨Όμ νμΈνλ κ²μ΄ λ ν¨μ¨μ μ΄μΌ.
π¨ μλͺ»λ μμ (μμΈλ₯Ό λ¨μ©ν μ½λ)
public class BadExample {
public static void main(String[] args) {
try {
int num = Integer.parseInt("abc"); // μ«μκ° μλ λ¬Έμμ΄ β μμΈ λ°μ
} catch (NumberFormatException e) {
System.out.println("μλͺ»λ μ
λ ₯μ
λλ€.");
}
}
}
β
"abc"
κ° μ«μκ° μλλΌ μμΈκ° λ°μνλλ°, μ΄λ° κ²½μ°λ μμΈλ₯Ό λ°μμν€μ§ μκ³ λ¨Όμ κ²μ¦νλ κ² μ’μ!
β μ±λ₯μ κ³ λ €ν μ½λ (μμΈλ₯Ό μ΅μνν μ½λ)
public class GoodExample {
public static void main(String[] args) {
String input = "abc";
if (input.matches("\\d+")) { // μ«μμΈμ§ λ¨Όμ νμΈ
int num = Integer.parseInt(input);
System.out.println("μ
λ ₯λ μ«μ: " + num);
} else {
System.out.println("μλͺ»λ μ
λ ₯μ
λλ€.");
}
}
}
π λΆνμν μμΈ μ²λ¦¬λ₯Ό νΌνκ³ , μ±λ₯μ κ°μ ν μ μμ΄!
β 2. Checked Exception vs Unchecked Exceptionμ μ μ ν νμ©
π μΈλΆ μμ(νμΌ, DB, λ€νΈμν¬ λ±)κ³Ό κ΄λ ¨λ μμΈλ Checked Exception μ¬μ©!
π μ λ ₯ κ° κ²μ¦, λ Όλ¦¬μ μ€λ₯ λ±μ Unchecked Exception μ¬μ©!
β 3. μμΈ λ©μμ§λ₯Ό λͺ ννκ² λ§λ€κΈ°
π‘ μμΈ λ°μ μ, λͺ νν λ©μμ§λ₯Ό μ 곡νλ©΄ λλ²κΉ μ΄ ν¨μ¬ μ¬μμ Έ!
public class CustomExceptionExample {
public static void main(String[] args) {
try {
validateAge(15);
} catch (IllegalArgumentException e) {
System.out.println("μ€λ₯: " + e.getMessage());
}
}
public static void validateAge(int age) {
if (age < 18) {
throw new IllegalArgumentException("λμ΄λ 18μΈ μ΄μμ΄μ΄μΌ ν©λλ€. μ
λ ₯κ°: " + age);
}
}
}
π μμΈ λ©μμ§μ μμΈκ³Ό μ λ ₯κ°μ ν¬ν¨νλ©΄ λ λΉ λ₯΄κ² μ€λ₯λ₯Ό ν΄κ²°ν μ μμ΄!
π μ¬ν μ§λ¬Έ (κΈ°μ λ©΄μ λλΉ)
β 1. Checked Exceptionκ³Ό Unchecked Exceptionμ μ°¨μ΄μ μ?
β
2. throw
μ throws
μ μ°¨μ΄μ μ?
β
3. finally
λΈλ‘μ΄ λ¬΄μ‘°κ±΄ μ€νλμ§ μλ κ²½μ°λ?
β
4. try-with-resources
λ μΈμ μ¬μ©νλ©΄ μ’μκΉ? (AutoCloseable
μΈν°νμ΄μ€)
β
5. catch(Exception e)
μ catch(Throwable t)
μ μ°¨μ΄μ μ?
π’ 11οΈβ£ μμΈ μ²λ¦¬ μ€μ μ μ© (Best Practices)
β 1. μμΈλ₯Ό λ¨λ°νμ§ μκΈ°
π‘ μμΈλ μ±λ₯ λΉμ©μ΄ ν¬κΈ° λλ¬Έμ, μμΈλ₯Ό λ¨μ©νλ©΄ μ±λ₯μ΄ μ νλ μ μμ΄!
π¨ μλͺ»λ μ½λ (λΆνμν μμΈ λ°μ)
public class BadExample {
public static void main(String[] args) {
try {
int[] arr = new int[5];
System.out.println(arr[10]); // μμΈ λ°μ
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("μλͺ»λ μΈλ±μ€ μ κ·Ό");
}
}
}
π μμΈκ° λ°μνλ©΄ μ±λ₯μ΄ μ νλ μ μμ΄.
π μ¬μ μ λ°©μ§ν μ μλ λ¬Έμ λΌλ©΄, μμΈ λμ 쑰건문μ νμ©νλ κ²μ΄ λ μ’λ€!
β μμΈ λ°μμ μ€μ΄λ μ½λ
public class GoodExample {
public static void main(String[] args) {
int[] arr = new int[5];
int index = 10;
if (index >= 0 && index < arr.length) { // 쑰건문μΌλ‘ μμΈ λ°©μ§
System.out.println(arr[index]);
} else {
System.out.println("μλͺ»λ μΈλ±μ€ μ κ·Ό");
}
}
}
π μμΈλ₯Ό λ°©μ§ν μ μμΌλ©΄ 쑰건문μ λ¨Όμ νμ©νμ!
β 2. μμΈ λ©μμ§λ μ΅λν ꡬ체μ μΌλ‘ μμ±νκΈ°
π‘ μμΈ λ©μμ§κ° ꡬ체μ μ΄λ©΄ λλ²κΉ μ΄ μ¬μμ§κ³ μ μ§λ³΄μκ° νΈν΄μ Έ!
π¨ μλͺ»λ μ½λ (μ λ§€ν μμΈ λ©μμ§)
throw new IllegalArgumentException("μ
λ ₯κ° μ€λ₯");
β μ’μ μ½λ (ꡬ체μ μΈ μμΈ λ©μμ§)
throw new IllegalArgumentException("λμ΄λ 18μΈ μ΄μμ΄μ΄μΌ ν©λλ€. μ
λ ₯λ κ°: " + age);
π μμΈ λ°μ μ, 무μμ΄ μλͺ»λμλμ§ λͺ ννκ² λ©μμ§λ₯Ό λ¨κΈ°μ!
β
3. λͺ¨λ μμΈλ₯Ό catchνμ§ μκΈ° (catch (Exception e)
)
π‘ λͺ¨λ μμΈλ₯Ό catch (Exception e)
λ‘ μ²λ¦¬νλ©΄, μ€μ μ€λ₯μ μμΈμ μ°ΎκΈ° μ΄λ €μ!
π¨ μλͺ»λ μ½λ
try {
int result = 10 / 0;
} catch (Exception e) { // λͺ¨λ μμΈλ₯Ό μ‘μΌλ©΄ μμΈμ μ°ΎκΈ° μ΄λ €μ
System.out.println("μμΈ λ°μ");
}
β μ’μ μ½λ (νΉμ μμΈλ§ μ²λ¦¬)
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("μ°μ μ€λ₯: 0μΌλ‘ λλ μ μμ΅λλ€.");
}
π κ°λ₯νλ©΄ λ°μν κ°λ₯μ±μ΄ λμ μμΈλ₯Ό κ°λ³μ μΌλ‘ μ²λ¦¬νλ κ²μ΄ μ’λ€!
π’ 12οΈβ£ μ΅μ μλ° κΈ°λ₯μ νμ©ν μμΈ μ²λ¦¬ μ΅μ ν
π‘ μλ° 7 μ΄ν μΆκ°λ μμΈ μ²λ¦¬ κΈ°λ₯μ νμ©νλ©΄ μ½λκ° λ κΉλν΄μ§ μ μμ΄!
β
1. λ€μ€ catch λΈλ‘μ |
μ°μ°μλ‘ λ¬ΆκΈ° (Java 7 μ΄μ)
π‘ κ°μ λ°©μμΌλ‘ μ²λ¦¬ν μμΈλΌλ©΄, |
μ°μ°μλ₯Ό μ¬μ©νλ©΄ μ½λκ° κ°κ²°ν΄μ Έ!
π¨ μλ° 7 μ΄μ (λΆνμνκ² λ°λ³΅λλ μ½λ)
try {
int num = Integer.parseInt("abc");
} catch (NumberFormatException e) {
System.out.println("μ«μλ‘ λ³νν μ μμ΅λλ€.");
} catch (NullPointerException e) {
System.out.println("λ κ°μ΄ ν¬ν¨λμ΄ μμ΅λλ€.");
}
β μλ° 7 μ΄ν (λ€μ€ μμΈ μ²λ¦¬ μ΅μ ν)
try {
int num = Integer.parseInt("abc");
} catch (NumberFormatException | NullPointerException e) {
System.out.println("μμΈ λ°μ: " + e.getMessage());
}
π μ€λ³΅ μ½λλ₯Ό μ€μ΄κ³ , κ°λ μ±μ λμΌ μ μμ΄!
β
2. try-with-resources
λ₯Ό νμ©ν΄ μμ μλ ν΄μ (Java 7 μ΄μ)
π‘ νμΌ, λ°μ΄ν°λ² μ΄μ€, λ€νΈμν¬ μ°κ²° λ±μ μ¬μ© ν λ°λμ λ«μμΌ ν΄!
β
try-with-resources
λ₯Ό μ¬μ©νλ©΄ μλμΌλ‘ λ«μμ€μ μ½λκ° κΉλν΄μ§κ³ , μμΈ μ²λ¦¬λ μμ ν΄!
π¨ μλ° 7 μ΄μ (μμ ν΄μ λ₯Ό μ§μ ν΄μΌ ν¨)
import java.io.*;
public class OldWay {
public static void main(String[] args) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("test.txt"));
System.out.println(br.readLine());
} catch (IOException e) {
System.out.println("νμΌ μ½κΈ° μ€λ₯");
} finally {
try {
if (br != null) br.close(); // μ§μ μμ ν΄μ
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
β
μλ° 7 μ΄ν (try-with-resources
μ¬μ©)
import java.io.*;
public class TryWithResourcesExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
System.out.println(br.readLine());
} catch (IOException e) {
System.out.println("νμΌ μ½κΈ° μ€λ₯");
}
}
}
π try-with-resources
λ₯Ό μ¬μ©νλ©΄ finally
μμ΄ μλμΌλ‘ μμμ΄ λ«νμ μμ νκ³ κΉλν΄!
β
3. 컀μ€ν
μμΈλ₯Ό λ§λ€ λ RuntimeException
μμ κ³ λ €νκΈ°
π‘ Checked Exceptionμ μ¬μ©ν μ§, Unchecked Exceptionμ μ¬μ©ν μ§ μ μ€νκ² μ ννμ!
- νμΌ, DB, λ€νΈμν¬ λ± β Checked Exception (
Exception
μμ) - μ
λ ₯κ° κ²μ¦, λ
Όλ¦¬ μ€λ₯ λ± β Unchecked Exception (
RuntimeException
μμ)
π¨ μλͺ»λ μμ (λΆνμν Checked Exception)
class InvalidAgeException extends Exception { // Checked Exception
public InvalidAgeException(String message) {
super(message);
}
}
β μ’μ μμ (Unchecked Exception)
class InvalidAgeException extends RuntimeException { // Unchecked Exception
public InvalidAgeException(String message) {
super(message);
}
}
π λΆνμν Checked Exceptionμ λ§λ€μ§ λ§κ³ , μν©μ λ§κ² μ ννμ!
π μ΅μ’ μ 리 (11~12λ¨κ³ ν΅μ¬ μμ½)
β μμΈλ₯Ό λ¨λ°νμ§ λ§κ³ , 미리 쑰건문μΌλ‘ λ°©μ§ν μ μλ μ€λ₯λ μ¬μ μ μ²λ¦¬νμ!
β μμΈ λ©μμ§λ₯Ό λͺ ννκ² μμ±νλ©΄ λλ²κΉ μ΄ ν¨μ¬ μ¬μμ§λ€.
β
λͺ¨λ μμΈλ₯Ό catch (Exception e)
λ‘ μ‘μ§ λ§κ³ , λ°μν κ°λ₯μ±μ΄ λμ μμΈλ§ μ‘μλΌ!
β
μλ° 7 μ΄ν μΆκ°λ κΈ°λ₯ (|
μ°μ°μ, try-with-resources
)λ₯Ό νμ©νλ©΄ μ½λκ° κΉλν΄μ§λ€.
β
컀μ€ν
μμΈλ₯Ό λ§λ€ λ RuntimeException
κ³Ό Exception
μ€ μ μ ν κ²μ μ ννμ.
π μ€μ λ©΄μ λλΉ μ§λ¬Έ (μμ©λ ₯ ν μ€νΈ)
β
try-with-resources
μ finally
λΈλ‘μ μ°¨μ΄μ μ?
β
catch (Exception e)
λ‘ λͺ¨λ μμΈλ₯Ό μ‘λ κ²μ΄ μ λμ μ΅κ΄μΌκΉ?
β
RuntimeException
μ μμνλ 컀μ€ν
μμΈλ₯Ό λ§λ€λ©΄ μ΄λ€ μ₯μ μ΄ μμκΉ?
β Checked Exceptionμ κ°μ νλ μ΄μ λ 무μμΌκΉ?
β μ±λ₯μ κ³ λ €ν μμΈ μ²λ¦¬λ₯Ό μ΄λ»κ² ν μ μμκΉ?
π’ 13οΈβ£ μ€μ μμ μ¬μ©νλ μμΈ μ²λ¦¬ λμμΈ ν¨ν΄
β 1. μμΈλ₯Ό λν(Wrapping)νμ¬ ν κ³³μμ μ²λ¦¬νκΈ°
π‘ μμ€ν μ΄ μ»€μ§μλ‘, κ°μ μμΈ μ²λ¦¬λ₯Ό μ¬λ¬ λ² λ°λ³΅νμ§ μλλ‘ μμΈλ₯Ό ν κ³³μμ κ΄λ¦¬νλ κ²μ΄ μ€μν΄!
π¨ μλͺ»λ μ½λ (μ€λ³΅λλ μμΈ μ²λ¦¬)
public void methodA() {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
throw new RuntimeException("λ©μλ Aμμ μ€λ₯ λ°μ: " + e.getMessage(), e);
}
}
public void methodB() {
try {
methodA();
} catch (RuntimeException e) {
System.out.println("λ©μλ Bμμ μ€λ₯ λ°μ: " + e.getMessage());
}
}
π methodA()
μμ λ°μν μμΈλ₯Ό methodB()
μμ λ μ²λ¦¬νκ³ μμ΄.
π μ΄λ° μμΌλ‘ νλ©΄ μ¬λ¬ κ³³μμ κ°μ μμΈλ₯Ό μ²λ¦¬ν΄μΌ ν΄μ μ μ§λ³΄μκ° μ΄λ €μμ Έ!
β μμΈλ₯Ό ν κ³³μμ μ²λ¦¬νλ μ½λ
public void methodA() throws ArithmeticException {
int result = 10 / 0;
}
public void methodB() {
try {
methodA();
} catch (ArithmeticException e) {
throw new CustomApplicationException("λΉμ¦λμ€ λ‘μ§ μ€λ₯ λ°μ", e);
}
}
public static void main(String[] args) {
try {
methodB();
} catch (CustomApplicationException e) {
System.out.println("μ΅μ’
μ€λ₯ μ²λ¦¬: " + e.getMessage());
}
}
π μμΈλ₯Ό CustomApplicationException
μΌλ‘ λνν΄μ ν κ³³μμλ§ μ²λ¦¬νλ©΄ μ½λκ° ν¨μ¬ κΉλν΄μ Έ!
β 2. μμΈ μ²λ¦¬μ© μ νΈλ¦¬ν° ν΄λμ€ νμ©
π‘ μμΈλ₯Ό μ²λ¦¬νλ κ³΅ν΅ μ νΈλ¦¬ν° ν΄λμ€λ₯Ό λ§λ€μ΄ μ€λ³΅ μ½λλ₯Ό μ€μ΄μ!
β μμΈ μ νΈλ¦¬ν° ν΄λμ€ λ§λ€κΈ°
public class ExceptionUtils {
public static void handleException(Exception e) {
System.err.println("μμΈ λ°μ: " + e.getMessage());
// λ‘κ·Έ νμΌ μ μ₯, μλ¦Ό μμ€ν
μ°λ κ°λ₯
}
}
β μμΈ λ°μ μ μ νΈλ¦¬ν°λ₯Ό νμ©νλ μ½λ
public class MyService {
public void execute() {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
ExceptionUtils.handleException(e);
}
}
}
π μ΄λ κ² νλ©΄ μμΈ μ²λ¦¬λ₯Ό ν κ³³μμ κ΄λ¦¬ν μ μκ³ , λ‘κΉ /μλ¦Ό κ°μ μΆκ° κΈ°λ₯λ μ½κ² μΆκ°ν μ μμ΄!
β 3. API μλ΅μ μν κΈλ‘λ² μμΈ μ²λ¦¬ (Spring μμ )
π‘ μΉ μλΉμ€μμλ μμΈ λ°μ μ, μ¬μ©μμκ² μ μ ν μλ΅μ μ£Όλ κ²μ΄ μ€μν΄!
β
Spring Bootμμλ @ControllerAdvice
λ₯Ό μ¬μ©ν΄ κΈλ‘λ² μμΈ μ²λ¦¬λ₯Ό ν μ μμ΄.
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ArithmeticException.class)
public ResponseEntity<String> handleArithmeticException(ArithmeticException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("μνμ μ€λ₯ λ°μ: " + e.getMessage());
}
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleGeneralException(Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("μλ² μ€λ₯ λ°μ: " + e.getMessage());
}
}
π μμΈκ° λ°μνλ©΄, νλ‘ νΈμλμ JSON νμμΌλ‘ μλ΅μ λ³΄λΌ μ μμ΄!
π μ΄λ κ² νλ©΄ μΌμΌμ΄ try-catch
λ‘ μμΈλ₯Ό μ²λ¦¬ν νμ μμ΄, μ€μμμ κ΄λ¦¬ν μ μμ΄.
π’ 14οΈβ£ μ΅μ νΈλ λ κΈ°λ°μ μμΈ μ²λ¦¬ μ΅μ ν
β 1. λ‘κΉ μ νμ©ν μμΈ λΆμ (Logback, SLF4J)
π‘ μμΈ λ°μ μ λ¨μν System.out.println
λμ λ‘κΉ
μ νμ©νλ©΄ μ μ§λ³΄μκ° μ¬μμ Έ!
β Logbackμ νμ©ν μμΈ λ‘κΉ
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LoggingExample {
private static final Logger logger = LoggerFactory.getLogger(LoggingExample.class);
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
logger.error("μνμ μ€λ₯ λ°μ", e);
}
}
}
π logger.error("μνμ μ€λ₯ λ°μ", e);
λ₯Ό μ¬μ©νλ©΄, μ€λ₯ λ©μμ§μ μ€ν νΈλ μ΄μ€λ₯Ό λ¨κΈΈ μ μμ΄!
π νμΌ λ‘κ·Έλ‘ μ μ₯νλ©΄, μλ² μ΄μ μ€ λ°μνλ μ€λ₯λ μ½κ² λΆμν μ μμ΄!
β 2. ν΄λΌμ°λ νκ²½μμ μμΈ λͺ¨λν°λ§ (Sentry, Datadog)
π‘ λκ·λͺ¨ μμ€ν μμλ μμΈλ₯Ό μ€μκ°μΌλ‘ λͺ¨λν°λ§ν μ μμ΄μΌ ν΄!
π Sentry, Datadog κ°μ λꡬλ₯Ό νμ©νλ©΄ μμΈλ₯Ό μλμΌλ‘ μΆμ νκ³ λΆμν μ μμ΄!
β Sentry μμ (Spring Boot)
import io.sentry.Sentry;
public class SentryExample {
public static void main(String[] args) {
Sentry.init(options -> {
options.setDsn("YOUR_SENTRY_DSN"); // Sentry μλ² μ°κ²°
});
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
Sentry.captureException(e); // μμΈλ₯Ό Sentryλ‘ μ μ‘
}
}
}
π μ΄λ κ² νλ©΄ μμΈ λ°μ μ, κ°λ°μκ° μ€μκ°μΌλ‘ μ€λ₯λ₯Ό κ°μ§νκ³ λΆμν μ μμ΄!
β 3. AI κΈ°λ° μμΈ λΆμ (GitHub Copilot, ChatGPT νμ©)
π‘ μ΅κ·Όμλ AIλ₯Ό νμ©ν΄μ μμΈ λ‘κ·Έλ₯Ό λΆμνκ³ ν΄κ²° λ°©λ²μ μ°Ύλ κ²μ΄ νΈλ λμΌ!
π AI μ½λ© λꡬλ₯Ό νμ©νλ©΄ μμΈ λ°μ μ μλμΌλ‘ ν΄κ²°μ± μ μ μλ°μ μλ μμ΄.
β GitHub Copilot νμ© μμ 1οΈβ£ μμΈκ° λ°μνλ μ½λλ₯Ό Copilotμ΄ μλμΌλ‘ μμ νλλ‘ μ λ
2οΈβ£ AIκ° μΆμ²νλ μμΈ μ²λ¦¬ λ°©μμ κΈ°λ°μΌλ‘ μμ
β ChatGPTλ₯Ό νμ©ν μμΈ λ‘κ·Έ λΆμ 1οΈβ£ λ°μν μμΈ λ©μμ§λ₯Ό ChatGPTμ μ λ ₯
2οΈβ£ ν΄κ²° λ°©λ²κ³Ό κ΄λ ¨ λ¬Έμλ₯Ό μλμΌλ‘ μΆμ²λ°μ
π AI κΈ°λ° μμΈ μ²λ¦¬λ μ μ λ λ°μ νκ³ μμΌλ©°, κ°λ° μμ°μ±μ ν¬κ² ν₯μμν¬ μ μμ΄!
π μ΅μ’ μ 리 (13~14λ¨κ³ ν΅μ¬ μμ½)
β μμΈλ₯Ό λν(Wrapping)νμ¬ ν κ³³μμ κ΄λ¦¬νλ©΄ μ μ§λ³΄μκ° μ¬μμ§λ€.
β κ³΅ν΅ μμΈ μ²λ¦¬ μ νΈλ¦¬ν° ν΄λμ€λ₯Ό λ§λ€μ΄ λ°λ³΅λλ μμΈ μ²λ¦¬λ₯Ό μ€μ΄μ.
β
Spring Bootμμλ @ControllerAdvice
λ‘ κΈλ‘λ² μμΈ μ²λ¦¬λ₯Ό νλ©΄ API μλ΅μ΄ κΉλν΄μ§λ€.
β λ‘κΉ (Logback, SLF4J)μ νμ©νλ©΄ μ΄μ μ€ λ°μνλ μμΈλ₯Ό μΆμ νκΈ° μ½λ€.
β ν΄λΌμ°λ νκ²½μμλ Sentry, Datadog λ±μ νμ©ν΄ μ€μκ° μμΈ λͺ¨λν°λ§μ νμ.
β AI κΈ°λ° μμΈ λΆμ(GitHub Copilot, ChatGPT)μ νμ©νλ©΄ μμΈ ν΄κ²° μλκ° λΉ¨λΌμ§λ€.
π μ΅μ’ λ§λ¬΄λ¦¬: μμΈ μ²λ¦¬ μμ μ 볡!
π― μ΄μ μμΈ μ²λ¦¬μ λͺ¨λ κ°λ κ³Ό μ€μ νμ© λ°©λ²μ λ°°μ μ΄!
π― κΈ°λ³Έ κ°λ λΆν° μ΅μ νΈλ λκΉμ§ μ΄ν΄νμΌλ, μ΄λ€ νλ‘μ νΈμμλ μμΈ μ²λ¦¬λ₯Ό μλ²½νκ² μ μ©ν μ μμ΄!
π νΉμ λ κΆκΈν μ μ΄ μμΌλ©΄ μΈμ λ μ§λ¬Έν΄ μ€! π π