8_1)
정의 : 프로그램 실행 시 발생할 수 있는 예외의 발생해 대비한 코드를 작성하는것
목적 : 프로그램의 비정상 종료를 막고 정상적인 실행상태를 유지
8_2)
4번
method1메서드가 method2메서드를 호출하였다.
8_3)
4번, 5번
상속받은 클래스의 메서드보다 예외를 더 많이 추가할 수 없다. 같거나 적어야함
Exception은 예외의 최고 조상이므로 Exception 하나 추가한다고 예외가 적은게 아니니 주의해야함
8_4)
1 -> 3 -> 5
- 캐치문에서 리턴해도 파이널리 블럭을 수행 한 후 리턴
- ArithmeticException은 RuntimeException의 자손이므로 RuntimeException이 정의된 캐치블럭에서 처리됨
1 -> 2 -> 5 -> 6
8_5)
3 -> 5
8_6)
1
- System.exit(0); 이 수행되면 프로그램이 즉시 종료되어 finally 블럭 수행 안됨
8_7)
class Excercise8_7 {
public static void main(String[] args) {
int answer = (int)(Math.random() * 100) + 1;
int input = 0;
int count = 0;
do {
count++;
try {
System.out.print("1과 100사이의 값을 입력하세요 :");
input = new Scanner(System.in).nextInt();
}catch (InputMismatchException e) {
// TODO: handle exception
System.out.println("숫자를 입력해주세요.");
continue;
}
if(answer > input) {
System.out.println(" 더 큰 수를 입력하세요.");
} else if(answer < input) {
System.out.println("더 작은 수를 입력하세요.");
} else {
System.out.println("맞췄습니다.");
System.out.println("시도횟수는 "+count+"번입니다.");
break;
}
} while(true);
}
}
8_8)
method1() 호출 -> method2() 호출 -> 오류 -> 2 출력 -> throw e -> 4출력 -> 7출력
결과 : 2 -> 4 -> 7
'Study > Java' 카테고리의 다른 글
Chapter09.java.lang 패키지 (0) | 2023.07.16 |
---|---|
Chapter07.객체지향 프로그래밍2 (1) | 2023.07.09 |
Chapter06.객체지향 프로그래밍1(2) (0) | 2023.07.09 |
Chapter06. 객체지향 프로그래밍I(1) (0) | 2023.06.25 |
03. 연산자 (0) | 2023.04.03 |
댓글