Exception 으로 사용 가능!
Exception 으로부터 상속된 것이기 때문
ArrayIndexOutOfBoundsException
배열의 크기가 오버가 되어지면서 발생하는 exception
ArithmeticException
분모에 0 이 들어가는 경우에 발생하는 exception
e.printStackTrace();
어디가 오류인지 추적해서 알려준다.
e.getMessage()
오류메시지
exception 처리 시 부모클래스의 exception 이 맨 아래에 나와야 한다.
Ex)
String[] str_arr = {"10", "스물", "30", "40"};
int cnt = 0;
for(int i=0; i<str_arr.length; i++) {
try{
int val = Integer.parseInt(str_arr[i]) + 1;
} finally {
System.out.println("[필수] >> " + ++cnt + "번 <<");
} // end of try~finally------------
} // end of for-------
// 결과
/*
11
[필수] >> 1번 <<
[필수] >> 2번 <<
java.lang.NumberFormatException 오류 발생
*/
Ex)
// 인터페이스
// 사용자가 정의한 exception -> Jango_lack_Exception
void order(int jumun_su) throws Jango_lack_Exception;
//========================================================//
// Product_imple 클래스
@Override // 인터페이스에 있는 메소드 재정의
public void order(int jumun_su) throws Jango_lack_Exception{
// 주문조건 나열
}
//========================================================//
// Main 클래스
try{
prod_arr[1].order(80); // 1번 80개 주문
} catch(Jango_lack_Exception e) {
System.out.println("예외메시지 : " + e.getMessage());
} // try~catch----------------
예외발생 : https://velog.io/@jjoung-2j/try-catch-%EB%AC%B8
my.day17.a.exception -> Exception_test
my.day17.b.user_define_exception
-> Product, Product_imple, Jango_lack_Exception, Main