我试图弄清楚为什么我必须throw
在 main 方法中出现异常,而我有try
/catch
块可以处理这些异常?即使我删除throws IllegalArgumentException,InputMismatchException
了一部分,程序仍然可以编译并完美运行。
public static void main(String[] args) throws IllegalArgumentException,InputMismatchException{
boolean flag = true;
Scanner in = new Scanner(System.in);
do{
try{
System.out.println("Please enter the number:");
int n = in.nextInt();
int sum = range(n);
System.out.println("sum = " + sum);
flag = false;
}
catch(IllegalArgumentException e){
System.out.println(e.getMessage());
}
catch(InputMismatchException e){
System.out.println("The number has to be as integer...");
in.nextLine();
}