为什么 try/catch InputMisMatchException 即使我导入 java.util.InputMisMatchException 也不起作用?
Why try/catch InputMisMatchException doesnt work even if I import java.util.InputMisMatchException?
我正在尝试编写简单的计算器并实现一些异常。如果客户端尝试输入字母而不是数字,我想捕获异常 InputMisMatchException。我已经导入了 java.util.Input...但是这仍然不起作用,它结束了程序。
import java.util.InputMismatchException;
import java.util.Scanner;
public class Calculator {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
boolean menu = true;
int choice;
while (menu) {
menuCalculator();
System.out.println();
System.out.println("Select operation: ");
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
try{
division();
} catch (InputMismatchException e){
System.err.println("Wrong input. Please enter valid value(s).");
}
break;
case 5:
System.out.println("Shutting down calculator");
menu = false;
break;
}
}
}
public static void menuCalculator() {
System.out.println("Press to go: \n 1. to divide \n 2. to multiplicate \n 3. to sum \n 4. to substract \n 5. to quit");
}
public static void division() {
double firstNo;
double secondNo;
double result;
System.out.println("Enter first number:");
firstNo = sc.nextDouble();
sc.nextLine();
System.out.println("Enter second number:");
secondNo = sc.nextDouble();
sc.nextLine();
result = firstNo / secondNo;
if (secondNo == 0) {
System.out.println("Cannot divide by 0");
} else {
System.out.println(firstNo + " / " + secondNo + " = " + result);
}
}
}
异常是由 nextInt
抛出的,但是您对 nextInt
的调用周围没有 try
/catch
,所以它没有得到抓住。移动 try
/catch
块,使 nextInt
调用在其中。 (您 是 处理来自 division
的 nextDouble
的错误,而不是来自 nextInt
。)
But:您可以考虑主动调用 hasNextInt
,而不是被动地处理异常。两种方法各有利弊。
以下是如何将 hasNextInt
与循环一起使用:
System.out.println("Select operation (1 - 5): ");
while (!sc.hasNextInt()) {
sc.nextLine();
System.out.println("Please entire a number [1 - 5]:");
}
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
// ...
或者也处理范围检查,例如:
do {
System.out.println("Select operation (1-5): ");
choice = -1;
if (!sc.hasNextInt()) {
System.out.println("Please enter a number (1-5, inclusive)");
} else {
choice = sc.nextInt();
if (choice < 1 || choice > 5) {
System.out.println(choice + " isn't an option, please enter a number (1-5, inclusive");
}
}
sc.nextLine();
} while (choice < 1 || choice > 5);
switch (choice) {
// ...
如果你想捕获异常,那么你必须包围接受扫描器输入的代码。您放错了 try-catch 块,然后它会起作用。
我正在尝试编写简单的计算器并实现一些异常。如果客户端尝试输入字母而不是数字,我想捕获异常 InputMisMatchException。我已经导入了 java.util.Input...但是这仍然不起作用,它结束了程序。
import java.util.InputMismatchException;
import java.util.Scanner;
public class Calculator {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
boolean menu = true;
int choice;
while (menu) {
menuCalculator();
System.out.println();
System.out.println("Select operation: ");
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
try{
division();
} catch (InputMismatchException e){
System.err.println("Wrong input. Please enter valid value(s).");
}
break;
case 5:
System.out.println("Shutting down calculator");
menu = false;
break;
}
}
}
public static void menuCalculator() {
System.out.println("Press to go: \n 1. to divide \n 2. to multiplicate \n 3. to sum \n 4. to substract \n 5. to quit");
}
public static void division() {
double firstNo;
double secondNo;
double result;
System.out.println("Enter first number:");
firstNo = sc.nextDouble();
sc.nextLine();
System.out.println("Enter second number:");
secondNo = sc.nextDouble();
sc.nextLine();
result = firstNo / secondNo;
if (secondNo == 0) {
System.out.println("Cannot divide by 0");
} else {
System.out.println(firstNo + " / " + secondNo + " = " + result);
}
}
}
异常是由 nextInt
抛出的,但是您对 nextInt
的调用周围没有 try
/catch
,所以它没有得到抓住。移动 try
/catch
块,使 nextInt
调用在其中。 (您 是 处理来自 division
的 nextDouble
的错误,而不是来自 nextInt
。)
But:您可以考虑主动调用 hasNextInt
,而不是被动地处理异常。两种方法各有利弊。
以下是如何将 hasNextInt
与循环一起使用:
System.out.println("Select operation (1 - 5): ");
while (!sc.hasNextInt()) {
sc.nextLine();
System.out.println("Please entire a number [1 - 5]:");
}
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
// ...
或者也处理范围检查,例如:
do {
System.out.println("Select operation (1-5): ");
choice = -1;
if (!sc.hasNextInt()) {
System.out.println("Please enter a number (1-5, inclusive)");
} else {
choice = sc.nextInt();
if (choice < 1 || choice > 5) {
System.out.println(choice + " isn't an option, please enter a number (1-5, inclusive");
}
}
sc.nextLine();
} while (choice < 1 || choice > 5);
switch (choice) {
// ...
如果你想捕获异常,那么你必须包围接受扫描器输入的代码。您放错了 try-catch 块,然后它会起作用。