문제 설명
ExceptionExam
클래스의 get50thItem
메소드에서는 매개변수로 받은 array의 50번째 값을 return합니다. 만약 array의 크기가 50보다 작을 경우에는 ArrayIndexOutOfBoundsException이라는 예외가 발생하는데요. get50thItem
이 ArrayIndexOutOfBoundsException를 throw
하도록 정의해 보세요.
throws
는 예외가 발생했을 때, 예외를 호출한 쪽에서 처리하도록 던져줍니다.
예를 들어, 아래의 코드에서는 메소드 선언 뒤에 throws ArithmeticException
이 적혀있는 것을 알 수 있습니다. 이렇게 적어놓으면 divide메소드는 ArithmeticException이 발생하니 divide메소드를 호출하는 쪽에서 오류를 처리하라는 뜻입니다.
public static void main(String[] args) {
int i = 10;
int j = 0;
try{
int k = divide(i, j);
System.out.println(k);
} catch(ArithmeticException e){
System.out.println("0으로 나눌수 없습니다.");
}
}
public static int divide(int i, int j) throws ArithmeticException{
int k = i / j;
return k;
}
실행 결과
실행 중지
실행 결과가 여기에 표시됩니다.