class Main {
public static void main(String[] args) {
try {
int rs = 계산기.나누다(10, 0);
System.out.println(rs);
}
catch ( ArithmeticException e ) {
System.out.println("0으로 나눌 수는 없습니다. ㅠㅠ");
}
catch ( Exception e ) {
System.out.println("알수 없는 에러가 발생하였습니다.");
}
}
}
class 계산기 {
static int 나누다(int a, int b) throws ArithmeticException {
int rs = a / b;
return rs;
}
}
throws
되어 처리된다.class Main {
public static void main(String[] args) {
int[] arr = new int[10];
채우다(arr);
for ( int i = 0; i < arr.length; i++ ) {
System.out.println("arr[" + i + "] => " + arr[i]);
}
}
static void 채우다(int[] arr) {
for ( int i = 0; i < 11; i++ ) {
try {
arr[i] = (i + 1) * 100;
}
catch ( ArrayIndexOutOfBoundsException e ) {
System.out.println("배열의 크기를 넘어선 index 사용이 감지되었습니다. 여기서 배열에 값 채워넣기 작업을 정지합니다.");
break;
}
}
}
}
int rs = 계산기.나누다(10, 0);
객체 생성을 안하고 바로 사용 됨.
계산기 클래스에서
나누다 함수는 static
으로 설정 해야한다.
static
을 사용하지 않을 시new 계산기()
인스턴스 생성하여 사용해야함.