Search

μ˜ˆμ™Έ 처리

μ˜ˆμ™Έ 처리

λ°œμƒν•œ μ˜ˆμ™Έμ— λŒ€ν•΄μ„œ ν”„λ‘œκ·Έλž¨ μ½”λ“œλ‘œ λŒ€μ‘ν•˜λŠ” 것

INDEX

1.
μ˜ˆμ™Έ 처리
β€’
μ˜ˆμ™Έ 처리 κ°œλ…
β€’
문법 (try-catch-finally)
β€’
μ£Όμš” μ˜ˆμ™Έ 클래슀
2.
μ—λŸ¬
β€’
컴파일 μ—λŸ¬
β€’
λŸ°νƒ€μž„ μ—λŸ¬
β—¦
μ—λŸ¬(Error)
β—¦
μ˜ˆμ™Έ(Exception)
3.
μ˜ˆμ™Έ 처리 방법
β€’
κΈ°λ³Έ μ˜ˆμ‹œ
β€’
닀쀑 catch
β€’
λ©€ν‹° catch
β€’
try-with-resources (AutoClose)
β€’
μ˜ˆμ™Έ μ „κ°€ (throws)
β€’
μ˜ˆμ™Έ κ°•μ œ λ°œμƒ (throw)
β€’
μ‚¬μš©μž μ˜ˆμ™Έ μ •μ˜

μ—λŸ¬

μ—λŸ¬λŠ” ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ 쀑 λ°œμƒν•˜λŠ” μ˜€μž‘λ™μœΌλ‘œ, 컴파일 μ—λŸ¬μ™€ λŸ°νƒ€μž„ μ—λŸ¬λ‘œ κ΅¬λΆ„λ©λ‹ˆλ‹€. 컴파일 μ—λŸ¬λŠ” μ½”λ“œ μž‘μ„± 였λ₯˜λ‘œ λ°œμƒν•˜κ³ , λŸ°νƒ€μž„ μ—λŸ¬λŠ” μ‹€ν–‰ 쀑 λ°œμƒν•˜λŠ” μ‹œμŠ€ν…œ λ¬Έμ œμž…λ‹ˆλ‹€.
β€’
μ—λŸ¬
β—¦
컴파일 μ—λŸ¬ (Compile Error)
β—¦
λŸ°νƒ€μž„ μ—λŸ¬ (Runtime Error)
β–ͺ
μ—λŸ¬ (Error)
β–ͺ
μ˜ˆμ™Έ (Exception)

1. 컴파일 μ—λŸ¬(Compile Error)

μ»΄νŒŒμΌν•˜λŠ” κ³Όμ •μ—μ„œ λ°œμƒν•˜λŠ” 였λ₯˜λ‘œ, 이 였λ₯˜κ°€ 있으면 ν”„λ‘œκ·Έλž¨μ΄ μ‹€ν–‰λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
β€’
νŠΉμ§•: ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ λΆˆκ°€
β€’
μ£Όμš” 원인:
β—¦
ν”„λ‘œκ·Έλž¨ 문법에 λ§žμ§€ μ•ŠλŠ” μ½”λ“œ μž‘μ„±
β—¦
μ‹€ν–‰λ¬Έ 끝에 ';' 기호 λˆ„λ½
β—¦
( ), { } κ΄„ν˜Έμ˜ 짝이 λ§žμ§€ μ•ŠλŠ” 경우
β—¦
μ„ μ–Έν•˜μ§€ μ•Šμ€ λ³€μˆ˜ μ‚¬μš© λ“±

2. λŸ°νƒ€μž„ μ—λŸ¬(Runtime Error)

ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ κ³Όμ •μ—μ„œ λ°œμƒν•˜λŠ” 였λ₯˜μž…λ‹ˆλ‹€. λŸ°νƒ€μž„ μ—λŸ¬λŠ” λ‹€μ‹œ 두 κ°€μ§€λ‘œ λ‚˜λˆŒ 수 μžˆμŠ΅λ‹ˆλ‹€

2.1. μ—λŸ¬(Error)

JVM, H/W λ“±μ˜ μ‹œμŠ€ν…œ 문제둜 인해 λ°œμƒν•˜λŠ” μ‹€ν–‰μ˜€λ₯˜μž…λ‹ˆλ‹€.
β€’
νŠΉμ§•: 볡ꡬ λ˜λŠ” μ²˜λ¦¬ν•  수 μ—†μŒ
β€’
μ˜ˆμ‹œ: OutOfMemoryError, StackOverflowError λ“±
β€’

2.2. μ˜ˆμ™Έ(Exception)

μ‚¬μš©μžμ˜ 잘λͺ»μ΄λ‚˜ 개발자의 잘λͺ»μœΌλ‘œ λ°œμƒν•˜λŠ” μ‹€ν–‰μ˜€λ₯˜μž…λ‹ˆλ‹€.
β€’
νŠΉμ§•: 볡ꡬ λ˜λŠ” 처리 κ°€λŠ₯함 β†’ μ˜ˆμ™Έ 처리(Exception Handling)
β€’
ꡬ뢄: Checked Exception(μ»΄νŒŒμΌλŸ¬κ°€ 체크), Unchecked Exception(Runtime Exception)
ν”„λ‘œκ·Έλž¨μ˜ μ•ˆμ •μ„±μ„ μœ„ν•΄μ„œλŠ” λ°œμƒ κ°€λŠ₯ν•œ μ˜ˆμ™Έλ₯Ό 적절히 μ²˜λ¦¬ν•˜λŠ” 것이 μ€‘μš”ν•©λ‹ˆλ‹€.

μ˜ˆμ™Έ 처리(Exception Handling)

λ°œμƒν•œ μ˜ˆμ™Έμ— λŒ€ν•΄μ„œ ν”„λ‘œκ·Έλž¨ μ½”λ“œλ‘œ λŒ€μ‘ν•˜λŠ” 것
ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ 쀑 λ°œμƒν•  수 μžˆλŠ” μ˜ˆμƒμΉ˜ λͺ»ν•œ 상황(μ˜ˆμ™Έ)을 μ²˜λ¦¬ν•˜κΈ° μœ„ν•œ λ©”μ»€λ‹ˆμ¦˜
μžλ°”μ—μ„œλŠ” μ˜ˆμ™Έκ°€ λ°œμƒν•˜λ©΄ ν”„λ‘œκ·Έλž¨μ΄ λΉ„μ •μƒμ μœΌλ‘œ μ’…λ£Œλ˜λŠ” 것을 λ°©μ§€ν•˜κ³ , 정상적인 μ‹€ν–‰ 흐름을 μœ μ§€ν•  수 μžˆλ„λ‘ ν•©λ‹ˆλ‹€.

문법

try { // μ˜ˆμ™Έκ°€ λ°œμƒν•  수 μžˆλŠ” μ½”λ“œ } catch (ExceptionType1 e1) { // ExceptionType1 μ˜ˆμ™Έ 처리 μ½”λ“œ } catch (ExceptionType2 e2) { // ExceptionType2 μ˜ˆμ™Έ 처리 μ½”λ“œ } finally { // 항상 μ‹€ν–‰λ˜λŠ” μ½”λ“œ (선택적) }
Java
볡사

μ£Όμš” μ˜ˆμ™Έ 클래슀

클래슀λͺ…
μ„€λͺ…
NullPointerExceptoin
null 을 κ°–λŠ” μ°Έμ‘° λ³€μˆ˜λ‘œ μ ‘κ·Ό μ—°μ‚°μž(.)λ₯Ό μ‚¬μš©ν–ˆμ„ λ•Œ λ°œμƒν•˜λŠ” μ˜ˆμ™Έ
ArrayIndexOutOfBoundsException
λ°°μ—΄μ—μ„œ λ²”μœ„λ₯Ό μ΄ˆκ³Όν•œ 인덱슀둜 μ ‘κ·Όν•  λ•Œ λ°œμƒν•˜λŠ” μ˜ˆμ™Έ
NumberFormatException
숫자둜 λ³€ν™˜ν•  수 μ—†λŠ” λ¬Έμžμ—΄μ„ 숫자둜 λ³€ν™˜ν•  λ•Œ λ°œμƒν•˜λŠ” μ˜ˆμ™Έ
ClassCastException
λΆ€λͺ¨/μžμ‹ 관계가 μ—†λŠ” 클래슀 간에 κ°•μ œλ‘œ νƒ€μž…λ³€ν™˜ μ‹œ λ°œμƒν•˜λŠ” μ˜ˆμ™Έ
ArithmeticException
숫자λ₯Ό 0으둜 λ‚˜λˆ„λŠ” λ“±μ˜ μ‚°μˆ μ—°μ‚° 관련에 λ¬Έμ œμ‹œ λ°œμƒν•˜λŠ” μ˜ˆμ™Έ

κΈ°λ³Έ μ˜ˆμ‹œ

λ‹€μŒμ€ μ˜ˆμ™Έ 처리의 기본적인 μ˜ˆμ‹œλ“€μž…λ‹ˆλ‹€:
β€’
ClassCastException - νƒ€μž… λ³€ν™˜ μ˜ˆμ™Έ
β€’
ArithmeticException - 0으둜 λ‚˜λˆ—μ…ˆ λ“± μ‚°μˆ  μ—°μ‚° μ˜ˆμ™Έ
β€’
IndexOutOfBoundsException - λ°°μ—΄ λ²”μœ„ 초과 μ˜ˆμ™Έ
β€’
InputMismatchException - μž…λ ₯ ν˜•μ‹ 뢈일치 μ˜ˆμ™Έ
β€’
NumberFormatException - 숫자 λ³€ν™˜ μ˜ˆμ™Έ

ClassCastException

try { Object obj = Integer.valueOf(42); String str = (String) obj; // ClassCastException λ°œμƒ } catch (ClassCastException e) { System.out.println("νƒ€μž… λ³€ν™˜ 였λ₯˜: " + e.getMessage()); }
Java
볡사

ArithmeticException (DivideByZero)

try { int result = 10 / 0; // ArithmeticException λ°œμƒ } catch (ArithmeticException e) { System.out.println("μ‚°μˆ  μ—°μ‚° 였λ₯˜: " + e.getMessage()); }
Java
볡사

IndexOutOfBoundsException

try { int[] arr = new int[5]; arr[10] = 25; // IndexOutOfBoundsException λ°œμƒ } catch (IndexOutOfBoundsException e) { System.out.println("λ°°μ—΄ 인덱슀 였λ₯˜: " + e.getMessage()); }
Java
볡사

InputMismatchException

import java.util.Scanner; import java.util.InputMismatchException; try { Scanner scanner = new Scanner(System.in); System.out.print("숫자λ₯Ό μž…λ ₯ν•˜μ„Έμš”: "); int num = scanner.nextInt(); // μ •μˆ˜κ°€ μ•„λ‹Œ κ°’ μž…λ ₯ μ‹œ InputMismatchException λ°œμƒ } catch (InputMismatchException e) { System.out.println("μž…λ ₯ ν˜•μ‹ 였λ₯˜: μ •μˆ˜λ₯Ό μž…λ ₯ν•΄μ•Ό ν•©λ‹ˆλ‹€."); }
Java
볡사

NumberFormatException

try { String str = "abc"; int num = Integer.parseInt(str); // NumberFormatException λ°œμƒ } catch (NumberFormatException e) { System.out.println("숫자 ν˜•μ‹ 였λ₯˜: " + e.getMessage()); }
Java
볡사

닀쀑 catch

μ—¬λŸ¬ μ˜ˆμ™Έλ₯Ό 각각 λ‹€λ₯Έ catch λΈ”λ‘μ—μ„œ μ²˜λ¦¬ν•˜λŠ” 것
λ‹€μ–‘ν•œ μ˜ˆμ™Έλ₯Ό 각각 λ‹€λ₯Έ λ°©μ‹μœΌλ‘œ μ²˜λ¦¬ν•˜κ³ μž ν•  λ•Œ μ—¬λŸ¬ 개의 catch 블둝을 μ‚¬μš©ν•©λ‹ˆλ‹€.
try { int[] arr = new int[5]; arr[10] = 10 / 0; } catch (ArithmeticException e) { System.out.println("μ‚°μˆ  μ—°μ‚° 였λ₯˜ λ°œμƒ"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("λ°°μ—΄ 인덱슀 였λ₯˜ λ°œμƒ"); } catch (Exception e) { System.out.println("기타 μ˜ˆμ™Έ λ°œμƒ: " + e.getMessage()); }
Java
볡사

λ©€ν‹° catch

β€’
μ„œλ‘œ λ‹€λ₯Έ μ˜ˆμ™Έλ₯Ό ν•˜λ‚˜μ˜ catch λΈ”λ‘μ—μ„œ λ¬Άμ–΄μ„œ μ²˜λ¦¬ν•˜λŠ” 것
Java 7λΆ€ν„° λ„μž…λœ κΈ°λŠ₯으둜, λ™μΌν•œ μ²˜λ¦¬κ°€ ν•„μš”ν•œ μ—¬λŸ¬ μ˜ˆμ™Έλ₯Ό ν•˜λ‚˜μ˜ catch λΈ”λ‘μ—μ„œ μ²˜λ¦¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
try { // μ˜ˆμ™Έ λ°œμƒ κ°€λŠ₯ μ½”λ“œ } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) { System.out.println("μ‚°μˆ  λ˜λŠ” λ°°μ—΄ 인덱슀 였λ₯˜: " + e.getMessage()); }
Java
볡사

try-with-resources (AutoClose)

μ˜ˆμ™Έ 처리 ν›„, μ‚¬μš©λœ λ¦¬μ†ŒμŠ€ 객체λ₯Ό μžλ™μœΌλ‘œ λ©”λͺ¨λ¦¬μ—μ„œ ν•΄μ œν•˜λŠ” 기법
Java 7λΆ€ν„° λ„μž…λœ try-with-resources ꡬ문을 μ‚¬μš©ν•˜λ©΄ μžμ›μ„ μžλ™μœΌλ‘œ 닫을 수 μžˆμŠ΅λ‹ˆλ‹€.
try (FileInputStream fis = new FileInputStream("file.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(fis))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { System.out.println("μž…μΆœλ ₯ 였λ₯˜: " + e.getMessage()); } // try 블둝이 μ’…λ£Œλ˜λ©΄ μžλ™μœΌλ‘œ brκ³Ό fisκ°€ λ‹«νž˜
Java
볡사

μ˜ˆμ™Έ μ „κ°€ (throws)

μ˜ˆμ™Έ 처리의 μ±…μž„μ„ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•œ 곳으둜 λ„˜κΈ°λŠ” 것
β€’
ν‚€μ›Œλ“œ : throws
a.k.a ) μ˜ˆμ™Έ λ˜μ§€κΈ°, μ˜ˆμ™Έ λ– λ„˜κΈ°κΈ°
λ©”μ†Œλ“œμ—μ„œ λ°œμƒν•  수 μžˆλŠ” μ˜ˆμ™Έλ₯Ό 직접 μ²˜λ¦¬ν•˜μ§€ μ•Šκ³  ν˜ΈμΆœν•œ 츑에 μ „κ°€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
public void readFile(String fileName) throws IOException { FileReader fr = new FileReader(fileName); BufferedReader br = new BufferedReader(fr); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } // ν˜ΈμΆœν•˜λŠ” μΈ‘μ—μ„œλŠ” μ΄λ ‡κ²Œ 처리 try { readFile("data.txt"); } catch (IOException e) { System.out.println("파일 읽기 였λ₯˜: " + e.getMessage()); }
Java
볡사

μ˜ˆμ™Έ κ°•μ œ λ°œμƒ

μ˜ˆμ™Έλ₯Ό κ°•μ œλ‘œ λ°œμƒμ‹œν‚€λŠ” 방법
β€’
ν‚€μ›Œλ“œ : throw
β€’
μ½”λ“œ μ‹€ν–‰ 흐름에 따라, μ˜ˆμ™Έλ₯Ό κ°•μ œλ‘œ λ°œμƒμ‹œμΌœ μ²˜λ¦¬ν•  λ•Œ μ‚¬μš©ν•œλ‹€.
// μ˜ˆμ™Έ κ°•μ œ λ°œμƒ μ˜ˆμ‹œ public class ExceptionExample { public static void main(String[] args) { try { validateAge(15); } catch (IllegalArgumentException e) { System.out.println("μ˜ˆμ™Έ λ°œμƒ: " + e.getMessage()); } System.out.println("ν”„λ‘œκ·Έλž¨ 계속 μ‹€ν–‰"); } public static void validateAge(int age) { if (age < 18) { throw new IllegalArgumentException("18μ„Έ λ―Έλ§Œμ€ μ΄μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€."); } System.out.println("인증 μ™„λ£Œ: λ‚˜μ΄ " + age); } } // μ‹€ν–‰ κ²°κ³Ό: // μ˜ˆμ™Έ λ°œμƒ: 18μ„Έ λ―Έλ§Œμ€ μ΄μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€. // ν”„λ‘œκ·Έλž¨ 계속 μ‹€ν–‰
Java
볡사

μ‚¬μš©μž μ˜ˆμ™Έ μ •μ˜

μžλ°” ν‘œμ€€ API μ—μ„œ μ œκ³΅ν•˜λŠ” μ˜ˆμ™Έ 클래슀 μ΄μ™Έμ—μ‚¬μš©μžκ°€ 직접 μ •μ˜ν•˜λŠ” μ˜ˆμ™Έ 클래슀
κΈ°μ‘΄ μ˜ˆμ™Έ 클래슀λ₯Ό ν™•μž₯ν•˜μ—¬ μ‚¬μš©μž μ •μ˜ μ˜ˆμ™Έλ₯Ό λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.
β€’
μΌλ°˜μ˜ˆμ™Έλ‘œ μ„ μ–Έν•˜λŠ” 경우 : Exception 클래슀λ₯Ό 상속
β€’
μ‹€ν–‰μ˜ˆμ™Έλ‘œ μ„ μ–Έν•˜λŠ” 경우 : RuntimeException 클래슀λ₯Ό 상속
// μ‚¬μš©μž μ •μ˜ μ˜ˆμ™Έ 클래슀 class InsufficientBalanceException extends Exception { private double amount; public InsufficientBalanceException(double amount) { super("μž”μ•‘ λΆ€μ‘±: " + amount + "원이 ν•„μš”ν•©λ‹ˆλ‹€."); this.amount = amount; } public double getAmount() { return amount; } } // μ‚¬μš© μ˜ˆμ‹œ class BankAccount { private double balance; public BankAccount(double initialBalance) { this.balance = initialBalance; } public void withdraw(double amount) throws InsufficientBalanceException { if (amount > balance) { throw new InsufficientBalanceException(amount - balance); } balance -= amount; System.out.println(amount + "원 좜금 μ™„λ£Œ. μž”μ•‘: " + balance + "원"); } } // μ˜ˆμ™Έ 처리 try { BankAccount account = new BankAccount(5000); account.withdraw(7000); // μ˜ˆμ™Έ λ°œμƒ } catch (InsufficientBalanceException e) { System.out.println("좜금 μ‹€νŒ¨: " + e.getMessage()); System.out.println("λΆ€μ‘±ν•œ κΈˆμ•‘: " + e.getAmount() + "원"); }
Java
볡사