Java 尝试捕获 inputMismatchException 错误
Java Try and Catch inputMismatchException Error
我有以下代码
//Ask for weight & pass user input into the object
System.out.printf("Enter weight: ");
//Check make sure input is a double
weight = input.nextDouble();
weight = checkDouble(weight);
System.out.println(weight);
System.exit(0);
方法 checkDouble 是
Double userInput;
public static Double checkDouble(Double userInput){
double weights = userInput;
try{
}catch(InputMismatchException e){
System.out.println("You have entered a non numeric field value");
}
finally {
System.out.println("Finally!!! ;) ");
}
return weights;
}
当我输入字母而不是数字时,我收到以下错误
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at HealthProfileTest.main(HealthProfileTest.java:42)
为什么错误的数据类型输入不会命中 catch 块中的 System.out.println() 行?
首先,你没有在 try 块中放置任何内容。
其次,如果你想放置任何引发异常的代码并且你想处理它,你应该把它放在 try-catch 块中。
Double userInput;
public static Double checkDouble(Double userInput){
try{
double weights = userInput;
}catch(InputMismatchException e){
System.out.println("You have entered a non numeric field value");
}
finally {
System.out.println("Finally!!! ;) ");
}
return weights;
}
Double weight = checkDouble(input);
public static Double checkDouble(Scanner userInput){
Double weights = null;
try{
weights = userInput.nextDouble();
}catch(InputMismatchException e){
System.out.println("You have entered a non numeric field value");
}
finally {
System.out.println("Finally!!! ;) ");
}
return weights;
}
正如您在 stacktrace 中看到的那样,它不是由您的方法抛出的,而是由 nextDouble()
调用抛出的:
at java.util.Scanner.nextDouble(Scanner.java:2456)
你在这里称呼它:
weight = input.nextDouble();
所以你应该通过 try catch 覆盖这部分:
try{
weight = input.nextDouble();
}catch(InputMismatchException e){
System.out.println("You have entered a non numeric field value");
}
finally {
System.out.println("Finally!!! ;) ");
}
InputMismatchException
由行 weight = input.nextDouble();
引发,它没有在 try
块内捕获,因此异常传播到您的 main
方法之外并崩溃程序。
你的 checkDouble()
方法实际上并没有 检查 任何东西,它只是期望一个空的 try
块来引发 InputMismatchException
(这是不可能的)。
您需要在 try
块中调用 nextDouble()
才能捕获异常。例如:
try {
double weight = input.nextDouble();
System.out.println(weight);
} catch (InputMismatchException e) {
System.out.println("Invalid number");
}
您可能更喜欢使用 Scanner.next()
从用户那里读取字符串,然后 然后 确定它是否是有效的双精度字符串。这样做的好处是可以为您提供用户的原始输入,即使它是无效的。
String weightText = input.next();
try {
double weight = Double.valueOf(weightText);
System.out.println(weight);
} catch (NumberFormatException e) {
System.out.println(weightText + " is not a valid double.");
}
我有以下代码
//Ask for weight & pass user input into the object
System.out.printf("Enter weight: ");
//Check make sure input is a double
weight = input.nextDouble();
weight = checkDouble(weight);
System.out.println(weight);
System.exit(0);
方法 checkDouble 是
Double userInput;
public static Double checkDouble(Double userInput){
double weights = userInput;
try{
}catch(InputMismatchException e){
System.out.println("You have entered a non numeric field value");
}
finally {
System.out.println("Finally!!! ;) ");
}
return weights;
}
当我输入字母而不是数字时,我收到以下错误
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at HealthProfileTest.main(HealthProfileTest.java:42)
为什么错误的数据类型输入不会命中 catch 块中的 System.out.println() 行?
首先,你没有在 try 块中放置任何内容。 其次,如果你想放置任何引发异常的代码并且你想处理它,你应该把它放在 try-catch 块中。
Double userInput;
public static Double checkDouble(Double userInput){
try{
double weights = userInput;
}catch(InputMismatchException e){
System.out.println("You have entered a non numeric field value");
}
finally {
System.out.println("Finally!!! ;) ");
}
return weights;
}
Double weight = checkDouble(input);
public static Double checkDouble(Scanner userInput){
Double weights = null;
try{
weights = userInput.nextDouble();
}catch(InputMismatchException e){
System.out.println("You have entered a non numeric field value");
}
finally {
System.out.println("Finally!!! ;) ");
}
return weights;
}
正如您在 stacktrace 中看到的那样,它不是由您的方法抛出的,而是由 nextDouble()
调用抛出的:
at java.util.Scanner.nextDouble(Scanner.java:2456)
你在这里称呼它:
weight = input.nextDouble();
所以你应该通过 try catch 覆盖这部分:
try{
weight = input.nextDouble();
}catch(InputMismatchException e){
System.out.println("You have entered a non numeric field value");
}
finally {
System.out.println("Finally!!! ;) ");
}
InputMismatchException
由行 weight = input.nextDouble();
引发,它没有在 try
块内捕获,因此异常传播到您的 main
方法之外并崩溃程序。
你的 checkDouble()
方法实际上并没有 检查 任何东西,它只是期望一个空的 try
块来引发 InputMismatchException
(这是不可能的)。
您需要在 try
块中调用 nextDouble()
才能捕获异常。例如:
try {
double weight = input.nextDouble();
System.out.println(weight);
} catch (InputMismatchException e) {
System.out.println("Invalid number");
}
您可能更喜欢使用 Scanner.next()
从用户那里读取字符串,然后 然后 确定它是否是有效的双精度字符串。这样做的好处是可以为您提供用户的原始输入,即使它是无效的。
String weightText = input.next();
try {
double weight = Double.valueOf(weightText);
System.out.println(weight);
} catch (NumberFormatException e) {
System.out.println(weightText + " is not a valid double.");
}