본문 바로가기
TIL

Checked Exception vs Unchecked Exception (22.11.03 TIL)

by winteringg 2022. 11. 3.

에러(Error) 와 예외(Exception)

프로그래밍에서 예외란, 입력 값에 대한 처리가 불가능하거나 프로그래밍 실행 중에 참조된 값이 잘못 된 경우 등 정상적인 프로그램의 흐름을 어긋나는 경우를 말한다. 예를 들어 배열의 범위를 벗어났다거나(ArrayIndexOutOfBoundsException), null 을 참조했다거나(NullPointerException), 존재하지 않는 파일의 이름을 입력한 경우 (FileNotFoundException) 등이 있다. 자바에서 예외는 개발자가 직접 처리할 수 있기 때문에 예외 상황을 미리 예측하여 핸들링할 수 있다.

반면 에러는 시스템에 무언가 비정상적인 상황이 발생한 경우이다. 주로 자바 가상 머신에서 발생하는 것이며, 예외와 반대로 이를 애플리케이션 코드에서 잡을 수 없다. 예를 들어, 메모리 부족(OutofMemoryError)이나 스택오버플로우(StackOverflowError)와 같이 미리 예측할 수 없는 경우를 말한다.

예외는 아래 그림과 같이 Checked Exception 과 Unchecked Exception 으로 구분할 수 있다. RuntimeException 을 상속하지 않은 클래스는 Checked Exception, 반대로 상속한 클래스는 Unchecked Exception 으로 분류한다.

Checked Exception vs Unchecked Exception

1. Checked Exception

RuntimeException 의 하위 클래스가 아닌 Exception 클래스의 하위 클래스들이다. 컴파일 타임에 확인되는 예외이며, 명시적인 예외 처리를 강제하기 때문에 checked 라고 한다. 반드시 try ~ catch 로 예외를 잡거나, throw 로 호출한 메서드에게 예외를 던져야 한다. 

2. Unchecked Exception

RuntimeException 의 하위 클래스들을 의미하는데, 명시적인 예외 처리를 강제하지 않기 때문에 Unchecked 라고 한다. 명시적인 예외 처리란 try ~ catch 로 예외를 잡거나 throw 로 호출한 메서드에게 예외를 던지는 행위이다.

 

 

참고 :

 

How to Handle Checked & Unchecked Exceptions in Java

The 2 Different Types of Java Exceptions are 1. Checked Exceptions that are recoverable 2. Unchecked Exceptions, that are fatal.

rollbar.com

 

 

Checked vs Unchecked Exceptions in Java - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

'TIL' 카테고리의 다른 글

옵셔널 체이닝 '?.' (22.11.05 TIL)  (0) 2022.11.05
Java - Stack 과 Queue (22.11.04 TIL)  (0) 2022.11.04
해시 함수란 (22.11.02 TIL)  (1) 2022.11.02
암호 기법 간단 분류 (22.11.01 TIL)  (0) 2022.11.01
토큰 기반 인증 (22.10.31 TIL)  (0) 2022.10.31

댓글