- throws
throws : 예외 처리
예외던지기 => 현재메서드를 호출한 메서드로 예외 전달.
📌
package chap9;
public class ExceptionEx4 {
public static void main(String[] args) {
try {
first();
} catch(Exception e) {
System.out.println("숫자만 가능합니다.");
e.printStackTrace();
}
}
private static void first() throws Exception{
System.out.println("first 메서드");
second();
}
//throws Exception : 예외가 발생하면 나를 호출한 메서드로 예외 전달.
private static void second() throws Exception {
System.out.println("second 메서드");
//int <= Integer.parseInt(숫자문자열)
try {
System.out.println(Integer.parseInt("abc"));//NumberFormatException 발생
} catch (Exception e) {
System.out.println("second:숫자만 입력하세요");
}
}
}
'수업(국비지원) > Java' 카테고리의 다른 글
| [Java] chap9 : 예외처리 - Exception6(오버라이딩에서 예외처리), Exception7(예외클래스 생성) (0) | 2023.04.16 |
|---|---|
| [Java] chap9 : 예외처리 - Exception5(throw) (0) | 2023.04.15 |
| [Java] chap9 : 예외처리 - Exception3(finally) (0) | 2023.04.15 |
| [Java] chap9 : 예외처리 - Exception1(try, catch), Exception2(다중 catch 구문) (0) | 2023.04.15 |
| [Java] chap8 : 인터페이스 - Exam1 (0) | 2023.04.15 |