[英]Why try/catch InputMisMatchException doesnt work even if I import java.util.InputMisMatchException?
I am trying to write simple calculator and implement some exceptions.我正在尝试编写简单的计算器并实现一些例外。 I want to catch exception InputMisMatchException if client will try to input letter instead number.
如果客户端尝试输入字母而不是数字,我想捕获异常 InputMisMatchException。 I have already import java.util.Input... but this still doesnt work and it end program.
我已经导入 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);
}
}
}
The exception is thrown by nextInt
, but you don't have a try
/ catch
around your call to nextInt
, so it doesn't get caught.异常由
nextInt
抛出,但您在调用nextInt
没有try
/ catch
,因此它不会被捕获。 Move your try
/ catch
block so that the nextInt
call is inside it.移动您的
try
/ catch
块,以便nextInt
调用在其中。 (You are handling the error from division
's nextDouble
, just not from nextInt
.) (您正在处理来自
division
的nextDouble
的错误,而不是来自nextInt
。)
But : You might consider calling hasNextInt
proactively, rather than dealing with the exception reactively.但是:您可能会考虑主动调用
hasNextInt
,而不是被动地处理异常。 Both approaches have pros and cons.这两种方法各有利弊。
Here's how you would use hasNextInt
with a loop:以下是将
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) {
// ...
or to handle range checking as well, something like:或者也处理范围检查,例如:
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) {
// ...
If you want to catch the Exception, then you have to surround the code that takes the Scanner input.如果要捕获异常,则必须将接受 Scanner 输入的代码括起来。 You have misplaced your try-catch block, then it will work.
您放错了 try-catch 块,然后它会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.