배열 크기 및 두 숫자를 입력받아 예외가 발생하면 이를 처리하는 문제입니다 이를 해결하는 다음 프로그램을 해석하세요
1) apple 2) banana 3) carrot 배열 허용 범위 초과 빈 객체 접근 오류
프로그램 시작
예외 처리
프로그램 종료
// 파일명 : ./Chapter15/ReferenceExceptions.java
import java.util.*;
public class ReferenceExceptions
{
// 프로그램 시작
1 public static void main( String args[] ) {
// 변수 초기화
String[] word = { "apple", "banana", "carrot" };
try {
2 for( int index = 0; index <= word.length; index++ ) {
3 System.out.println( ( index + 1 ) + ") " + word[ index ] + " " );
}
}
// 예외 처리
4 catch ( ArrayIndexOutOfBoundsException e ) {
5 System.out.println( "\n배열 허용 범위 초과" );
}
try {
// null 포인터 예외 발생
6 word = null;
7 System.out.println( word[0] );
}
8 catch ( NullPointerException e ) {
9 System.out.println( "빈 객체 접근 오류" );
}
// 프로그램 종료
10 }
}
※ 실행순서 및 메모리상태는 A키(이전) 및 D키(다음)를 눌러도 확인할 수 있습니다