목차
- throw 예제
- throw 정의
- throw / throws 차이점
throw 예제
Hz.java
public class Hz {
static void checkLevel(int level) {
if (level < 3) {
throw new ArithmeticException("등업 후 이용 바람.");
} else {
System.out.println("입장 가능.");
}
}
public static void main(String[] args) {
checkLevel(2);
}
}
결과값:
Exception in thread "main" java.lang.ArithmeticException: 등업 후 이용 바람.
at Hz.checkLevel(Hz.java:4)
at Hz.main(Hz.java:11)
throw 정의
exception (= 예외 = 사용자 지정 오류) 던지기.
※ 던지기 = 내뱉기 = 생성
1.
throw 문은 exception (=사용자 지정 오류) 유형과 함께 사용.
(예)
ArithmeticException,
ClassNotFoundException,
ArrayIndexOutOfBoundsException,
SecurityException
2.
exception 유형은 '사용자 지정 메서드'와 함께 자주 사용.
※ 위 예제 참고.
3.
try...catch... 구문 안에서 exception 다룰 때 주로 사용..
throw / throws 차이점
[throw]
1.
메서드에 대한 exception (= 예외 = 사용자 지정 오류) 던짐.
2.
여러 exception (= 예외 = 사용자 지정 오류) throw 불가능.
3.
뒤에 object (= 객체 = new 붙은 형태)가 옴.
4.
메서드 안에서 사용.
[throws]
1.
메서드가 throw 할 수 있는 exception (=예외 = 사용자 지정 오류) 유형을 나타냄.
2.
여러 exception (= 예외 = 사용자 지정 오류) 선언 가능.
3.
뒤에 class (= 클래스)가 옴.
4.
메서드 signature(서명)과 함께 사용됨.
최신댓글