class Main {
public static void main(String[] args) {
int[] datas = new int[2];
try {
work(datas);
}
catch ( ArrayIndexOutOfBoundsException e ) { // main 함수 입장에서 이 코드는 가독성이 떨어진다.
System.out.println("이런.. 오류가 발생했군요.");
}
}
static void work(int[] datas) {
datas[0] = 10;
datas[1] = 20;
datas[2] = 30; // 여기서 자동으로 throw new ArrayIndexOutOfBoundsException(); 이 발생한다.
}
}
throw new
배열의_사이즈_오류_Exception
으로 자진 신고
static void work(int[] datas) {
if ( datas.length < 3 ) {
throw new 배열의_사이즈_오류_Exception(); // 함수가 여기서 멈춤
}
datas[0] = 10;
datas[1] = 20;
datas[2] = 30;
}
class 배열의_사이즈_오류_Exception extends RuntimeException { }
runtime
런타임