例: try...catch 条件文の final 修飾子、名前、さらには例外の種類を設定できます。
サンプル
次の例では、 int number = scanner.nextInt(); の Java 文を選択し、 try / catch の surround ステートメントを適用します。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
System.out.println("You entered: " + number);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = 0;
try {
number = scanner.nextInt();
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("You entered: " + number);
}
}