Java 中的 try/catch
有人可以给我提示为什么这个尝试和捕获不起作用吗? 它抛出扫描仪异常,而不是打印我期望的消息。
import java.util.*;
import java.io.*;
import java.math.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
Boolean test = true;
while (test == true) {
try {
double x, y;
String operator;
Scanner scan = new Scanner(System.in);
Scanner scan_2 = new Scanner(System.in);
Scanner ScanOperator = new Scanner(System.in);
System.out.println(" Enter a double value: ");
x = scan.nextDouble();
System.out.println(" Enter another double value: ");
y = scan_2.nextDouble();
System.out.println(" Enter a operator for the operation you want to execute, or X if you want to quit: ");
operator = ScanOperator.nextLine();
if (operator.equals("x") || operator.equals("X")) {
test = false;
System.out.println("No calculation was made!!!");
}
System.out.println(Calculation(operator, x, y));
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null,"Input must be a number.");
}
}
}
public static double Calculation(String operator, double x, double y) {
double result = 0;
double myAdd = 0;
double mySub = 0;
double myMult = 0;
double myDiv = 0;
double myPower = 0;
double myMod = 0;
if (operator.equals("+")) {
myAdd = x + y;
result = myAdd;
} else if (operator.equals("-")) {
mySub = x - y;
result = mySub;
} else if (operator.equals("*")) {
myMult = x * y;
result = myMult;
} else if (operator.equals("/")) {
myDiv = x / y;
result = myDiv;
} else if (operator.equals("^")) {
myPower = Math.pow(x, y);
result = myPower;
} else if (operator.equals("%")) {
myMod = x % y;
result = myMod;
} else {
}
return result;
}
}
Could someone please give me a hint why this try and catch is not working?
It throws a scanner exception instead of printing the message I expect.
import java.util.*;
import java.io.*;
import java.math.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
Boolean test = true;
while (test == true) {
try {
double x, y;
String operator;
Scanner scan = new Scanner(System.in);
Scanner scan_2 = new Scanner(System.in);
Scanner ScanOperator = new Scanner(System.in);
System.out.println(" Enter a double value: ");
x = scan.nextDouble();
System.out.println(" Enter another double value: ");
y = scan_2.nextDouble();
System.out.println(" Enter a operator for the operation you want to execute, or X if you want to quit: ");
operator = ScanOperator.nextLine();
if (operator.equals("x") || operator.equals("X")) {
test = false;
System.out.println("No calculation was made!!!");
}
System.out.println(Calculation(operator, x, y));
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null,"Input must be a number.");
}
}
}
public static double Calculation(String operator, double x, double y) {
double result = 0;
double myAdd = 0;
double mySub = 0;
double myMult = 0;
double myDiv = 0;
double myPower = 0;
double myMod = 0;
if (operator.equals("+")) {
myAdd = x + y;
result = myAdd;
} else if (operator.equals("-")) {
mySub = x - y;
result = mySub;
} else if (operator.equals("*")) {
myMult = x * y;
result = myMult;
} else if (operator.equals("/")) {
myDiv = x / y;
result = myDiv;
} else if (operator.equals("^")) {
myPower = Math.pow(x, y);
result = myPower;
} else if (operator.equals("%")) {
myMod = x % y;
result = myMod;
} else {
}
return result;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
很简单,程序抛出了ScannerException,但是你的try catch只能捕获NumberFormatException,你需要添加另一个catch子句才能捕获ScannerException,或者只捕获通用的Exception。
例如,当您说:
那只是指定如何捕获 NumberFormatException。
为了捕获所有异常,您需要这样做:
在这种情况下,第二个 catch 将获取第一个 catch 中未捕获的所有内容,因为所有异常都扩展了 Exception 类,您可以使用该语句捕获所有派生类。
但是,由于捕获异常本身是不受欢迎的,因此您也可以这样做:
在同一块中捕获两个异常。
Simple, the program throws ScannerException, but your try catch can only catch NumberFormatException, you need to add another catch clause in order to catch ScannerException, or catch only the generic Exception.
for example, when you say:
that is only specifying how to catch NumberFormatException.
In order to catch all exceptions, you would need to make it:
In this case, the second catch would get everything that was not caught in the first catch because all exceptions extend the Exception class, you can catch all derived classes with that statement.
However, since catching Exception by itself is frowned upon, you could also do:
To catch both exceptions in the same block.
您正在尝试捕获 NumberFormatException。您需要为 ScannerException 添加 catch 语句,因为它与 NumberFormatException 不同。
You're attempting to catch a NumberFormatException. You need to add a catch statement for a ScannerException, as it is different from a NumberFormatException.
您需要捕获 ScannerException 或类似的异常。
在此代码中,您仅捕获 NumberFormatException 。
尝试一些这样的:
You need to catch a ScannerException or some like this.
At this code you are only catching the NumberFormatException .
Try some like this:
您捕获了错误的异常。
You're catching the wrong exception.
您的代码不会抛出
NumberFormatException
。您应该改为捕获InputMismatchException
。查看
nextDouble
,在Scanner
中,似乎Scanner
代码为您处理NumberFormatException
,然后抛出一个不同类型的异常:来自
java.util.Scanner
:当您遇到这样的问题时,我建议您首先查看 Java 源代码。这是一个很好的资源。
另请注意,JDK 中没有
ScannerException
。Your code will not throw a
NumberFormatException
. You should catch anInputMismatchException
instead.Looking at
nextDouble
, inScanner
, it seems that theScanner
code handles theNumberFormatException
for you and then throws a different type of exception:from
java.util.Scanner
:When you hit a problem like this, I recommend that you look through the Java source as a first stop. It is a great resource.
Also note that there is no
ScannerException
in the JDK.只需捕获
InputMismatchException
而不是NumberFormatException
即可,一切正常。Just catch
InputMismatchException
instead ofNumberFormatException
and everything works fine.为什么不这样做:
仅供参考,双精度的实际正则表达式将是 [digit][.][digit],其中 [.][digit] 是可选的。
Why not just do:
FYI, the actual regex for double precision would be [digit][.][digit] with the [.][digit] being optional.