문제
배열 크기 및 두 숫자를 입력받아 예외가 발생하면 이를 처리하는 문제입니다 이를 해결하는 프로그램의 다음 실행상태에 대해 빈칸을 채우세요
프로그램의 실행순서 및 실행상태
1 public static void main( String args[] ) {
2 for( int index = 0; index <= word.length; index++ ) {
3 System.out.println( ( index + 1 ) + ") " + word[ index ] + " " );
1) apple
2 for( int index = 0; index <= word.length; index++ ) {
3 System.out.println( ( index + 1 ) + ") " + word[ index ] + " " );
2) banana
2 for( int index = 0; index <= word.length; index++ ) {
3 System.out.println( ( index + 1 ) + ") " + word[ index ] + " " );
3) carrot
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배열 허용 범위 초과" );
배열 허용 범위 초과
6 word = null;
7 System.out.println( word[0] );
8 catch ( NullPointerException e ) {
9 System.out.println( "빈 객체 접근 오류" );
빈 객체 접근 오류
10 }
프로그램 코드
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 }
}