java unchecked 예제
public class UncheckedExample {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        int index = 6;

        try {
            int value = numbers[index];
            System.out.println("Value: " + value);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Index is out of bounds!");
        }
    }
}

위의 예제는 자바에서 “ArrayIndexOutOfBoundsException”과 같은 unchecked 예외를 처리하는 방법을 보여줍니다. 이 예제에서는 numbers 배열의 길이보다 큰 index값을 사용하여 값을 얻으려고 시도합니다.

try-catch 문을 사용하여 ArrayIndexOutOfBoundsException 예외를 처리하고, 예외가 발생할 경우 “Index is out of bounds!”라는 메시지를 출력합니다.

이와 같은 unchecked 예외는 컴파일러가 확인하지 않으므로, 예외 처리 코드를 작성하여 프로그램의 안정성을 높일 수 있습니다.

#java #unchecked예외