Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter02 : exception 본문

JAVA/chapter19_exception

chapter02 : exception

GAWON 2023. 5. 30. 09:28
package org.joonzis.ex;
/*
 * ArrayIndexOutOfBoundsException
 * 배열에서 인덱스 범위를 초과하는 경우 예외 발생
 */
public class Ex02_exception {
	public static void main(String[] args) {
		
		/*	크기 3 짜리 정수형 배열 arr 를 선언
		 * 각 인덱스에 순서대로 1, 2, 3 데이터 삽입
		 * 향상 for문을 이용하여 배열 내 데이터 출력
		 * 
		 * *** 예외 처리 ***
		 * 에러 발생 시
		 * "인덱스 가용 범위를 벗어났습니다." 출력
		 */
		
		int[] arr = new int[3];
		
		try {
			arr[0] = 1;
			arr[1] = 2;
			arr[3] = 3;	// 해당 위치에서 예외 발생
			
			for(int n : arr) {
				System.out.println(n);
			}
		} catch (ArrayIndexOutOfBoundsException e) {
			//e.printStackTrace();
			System.out.println("인덱스 가용 범위를 벗어났습니다.");
		}
		
	
		
		
		
	}
}

'JAVA > chapter19_exception' 카테고리의 다른 글

chapter06 : exception  (0) 2023.05.30
chapter05 : exception  (0) 2023.05.30
chapter04 : exception  (0) 2023.05.30
chapter03 : exception  (0) 2023.05.30
chapter01 : exception  (0) 2023.05.30