例如,您可以配置 final 修饰符、名称甚至异常的类型,以用于 try...catch 条件语句。
示例
在以下示例中,我们选择 int number = scanner.nextInt(); Java 语句,并应用 try / catch 包围语句。
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);
}
}