Java

Java Exception

승모근뭉치 2023. 1. 29. 00:22

프로젝트 내 src 내 javabasic 패키지 내 Ex15Exception.java

package javabasic;

public class Ex15Exception {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] data = { 3, 2, 5, 6 };
		for (int i = 0; i <= data.length; i = i + 1) {
			try {
				System.out.println(data[i]);
				System.out.println("--------");
			} catch (ArrayIndexOutOfBoundsException e) {
				// 에러가 나는 라인번호를 추적하고싶으면
				e.printStackTrace();
				System.out.println("배열범위 오류:" + e.getMessage());
			}
		}
		System.out.println("** 종료 **");
	}

}

실행 결과

3
--------
2
--------
5
--------
6
--------
java.lang.ArrayIndexOutOfBoundsException: 4
	at javabasic.Ex15Exception.main(Ex15Exception.java:10)
배열범위 오류:4
** 종료 **