문제
배열 크기 및 두 숫자를 입력받아 예외가 발생하면 이를 처리하는 문제입니다 실행순서를 클릭하세요
1) apple
2) banana
3) carrot
배열 허용 범위 초과
빈 객체 접근 오류
프로그램 코드
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 {
6 word = null;
7 System.out.println( word[0] );
}
8 catch ( NullPointerException e ) {
9 System.out.println( "빈 객체 접근 오류" );
}
10 }
}