とにかくそれらの例外を処理できる/ブロックがあるthrow
のに、メインメソッドで例外を発生させる必要がある理由を理解しようとしていますか? 一部を削除しても、プログラムはコンパイルされ、完全に動作します。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();
}