카테고리 없음
예외 처리 문제
일상코더
2022. 10. 31. 10:56
문제 1.
1. 자바에서는 상속을 이용해서 모든 예뢰를 표현한다. 모든 예외 클래스는 ( )의 자손 클래스 이다.
문제 2.
divide() 함수는 매개변수(parameter)에 들어오는 값에 따라서 ArithmeticException과 ArrayIndexOutOfBoundsException
이 발생할 수 있다.
Main 함수에서 try-catch 문을 이용해서 , 다음 동작을 구현하세요.
a. ArithmeticException 이 발생할 대는 잘못된 계산임을 알리는 문구를 출력하세요.
b. ArrayIndexOutOfBoundsExceptin 이 발생할 대는 현재 배열의 index범위를 알려주는 문구를 출력하세요
class ArrayCalculation {
int[] arr = { 0, 1, 2, 3, 4 };
public int divide(int denominatorIndex, int numeratorIndex)
throws ArithmetciException, ArrayIndexOutOfBoundsException{
return arr[denominatorIndex] / arr[numeratorIndex];
}
}
public class Main {
public static void main(String[] args) {
ArrayCalculation arrayCalculation = new ArrayCalculation();
System.out.println("1 / 0 = " + arrayCalculation.divide(1, 0)); // java.lang.ArithmeticException: "/ by zero"
System.out.println("Try to divide using out of index element = "
+ arrayCalculation.divide(5, 0)); // java.lang.ArrayIndexOutOfBoundsException: 5
}
}