inblog logo
|
sxias
    자바

    [JAVA] 49. 예외 처리

    문정준's avatar
    문정준
    Feb 18, 2025
    [JAVA] 49. 예외 처리
    Contents
    예외 처리를 사용하는 이유

    예외 처리를 사용하는 이유

    • 모든 오류를 사전에 잡을 수 없음
    • 예상치 못한 오류를 디버깅 : 처리하기 위해
     
    package ex15; public class DivideByZeroOK { public static void main(String[] args) { try { int result = 10 / 0; System.out.println(result); } catch (Throwable e) { System.out.println("괜찮아" + e.getMessage()); } } }
    ✅
    10 / 0은 통상적으로 계산할 수 없음 (무한대)
    • 0 나누기의 오류는 ArithmeticException이라는 예외 투척 : 프로그램 중단
    Throwable에 있는 getMessage()를 ArithmeticException에서 호출해도 작동 가능
    • ArithmeticException은 자식 클래스, Throwable 클래스는 부모 클래스
      • ArithmeticException < RuntimeException < Throwable
    Share article
    Contents
    예외 처리를 사용하는 이유

    Sxias ㆍ a32176740@gmail.com

    RSS·Powered by Inblog