자바 예외처리 throws
-
게시물 수정 , 삭제는 로그인 필요
try catch문은 이해가 되는데 22번째 줄 public static int numberInput() throws InputMismatchException 메서가 이해가 안돼요 ㅠㅠ 왜 작성하는 건가요?? 앞에 try catch문으로 되지 않나요?
package Day15;
import java.util.InputMismatchException;
import java.util.Scanner;
public class EX12 {
public static void main(String[] args) {
// 사용자로부터 정수를 입력 받아 반환해준다 = numberInput()
System.out.print("정수를 입력하세요 : ");
try {
int number = numberInput();
System.out.println("입력값 : \"" + number + "\" ");
} catch (InputMismatchException e) {
System.err.println(e);
}
}
// 정적 메서드
// 예외를 전가시키는 것 = throws
public static int numberInput() throws InputMismatchException {
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
scanner.close();
return number;
}
}
package Day15;
import java.util.InputMismatchException;
import java.util.Scanner;
public class EX12 {
public static void main(String[] args) {
// 사용자로부터 정수를 입력 받아 반환해준다 = numberInput()
System.out.print("정수를 입력하세요 : ");
try {
int number = numberInput();
System.out.println("입력값 : \"" + number + "\" ");
} catch (InputMismatchException e) {
System.err.println(e);
}
}
// 정적 메서드
// 예외를 전가시키는 것 = throws
public static int numberInput() throws InputMismatchException {
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
scanner.close();
return number;
}
}
#자바 예외처리 #자바 예외처리 종류 #자바 예외처리 예제 #자바 예외처리 throw #자바 예외처리 방법 #자바 예외처리 만들기 #자바 예외처리 try catch #자바 예외처리 코드 #자바 예외처리 클래스 #자바 예외처리 여러개