728x90

 

finally 블록을 추가하면 try문에서 return을 해도 무조건 실행된다!

public class Ex4 {

	public static void main(String[] args) {
		try {
			inputScore(101);
		} catch (InvalidScoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// throws로 예외처리의 책임을 상위로 위임함
	public static void inputScore(int score) throws InvalidScoreException {

		if (score < 0 || score > 100) {
        	// 사용자정의 예외클래스로 예외 발생
			throw new InvalidScoreException("0 ~ 100사이의 정수만 입력하셈");
		}
	}

}

// Exception(상위 예외클래스)를 상속하는 사용자정의 예외클래스
class InvalidScoreException extends Exception {

	public InvalidScoreException(String message) {
		super(message);
		// TODO Auto-generated constructor stub
	}

}

+ Recent posts