为什么我会收到这个只有类型错误而不是 zeroDivision 错误的奇怪错误?
Why am I getting this weird bug with only a type error and not a zeroDivision error?
所以我真的有两个问题。第一个是,在 Java 中,我如何执行与此 python 代码等效的操作:
while True:
try:
num1 = int(input('Enter num1: '))
num2 = int(input('Enter num2: '))
print(str(num1) + ' / ' + str(num2) + ' = ' + str(num1 / num2))
break
except:
print('ERROR')
我想要的是程序要对用户说的 "No you did this wrong! Now do it again until you get it right." 我试过在 java 中写这个:
import java.util.Scanner;
public class TryCatch {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
try {
System.out.print("Enter num1: ");
int num1 = input.nextInt();
System.out.print("Enter num2: ");
int num2 = input.nextInt();
System.out.println(num1 + " / " + num2 + " = " + num1 / num2);
break;
} catch (Exception e) {
System.out.println(e);
System.out.println(e.getCause());
System.out.println(e.getMessage());
}
}
}
}
现在,当 num2 等于 0 时,这也正是我想要的。作为参考,这是发生这种情况时的输出,我在第一次迭代后终止了进程:
Enter num1: 5
Enter num2: 0
java.lang.ArithmeticException: / by zero
null
/ by zero
Enter num1:
Process finished with exit code 1
然而,当 num1 或 num2 是字符串时,java 会在无限循环中打印以下 3 行,直到我终止进程:
java.util.InputMismatchException
null
null
这显然意味着我做错了什么,但对于我的一生,我不知道为什么 catch 块将 运行ning 保持在一个无限循环中,但没有 zeroDivision 类型错误错误...
所以我的 2 个具体问题是:
- 如何实现我所描述的我想做的事情?即让程序告诉用户 "No you did this wrong! Now do it again until you get it right."
- 这是一个不正确的解决方案的原因是什么?即,当存在类型错误但没有 zeroDivision 错误时,为什么 catch 块 运行 处于无限循环中?
编辑:
由于我只能接受 1 个答案,我想特别感谢 @rgettman 解释 nextInt() 方法如何导致我的特定问题,并感谢 @Alireza Dastyar 为我提供了我认为更优雅的解决方案,因为它改变了抛出异常的位置from 而不是在事后清理混乱的输入,这意味着当没有抛出特定异常时你已经给出了虚拟输入。
执行永远不会被零除。当您输入 nextInt()
无法处理的内容时,该方法会在执行到达除法表达式之前抛出 InputMismatchException
。
此外,当使用输入的 Scanner
方法抛出该异常时,它不会使用有问题的输入,循环重复,并且 nextInt()
在 相同 输入,导致无限循环。
nextInt()
javadocs:
This method will throw InputMismatchException
if the next token cannot be translated into a valid int value as described below. If the translation is successful, the scanner advances past the input that matched.
- 在
catch
块中,将消息打印给用户重试。
- 在
catch
块中调用 input.next()
以处理不正确的输入并前进到下一个标记。
首次输入错误时,您需要谨慎处理;在这种情况下,可能有 2 个输入标记要跳过。
考虑调用 hasNextInt()
以确定输入中是否有要解析的整数。
所以我真的有两个问题。第一个是,在 Java 中,我如何执行与此 python 代码等效的操作:
while True:
try:
num1 = int(input('Enter num1: '))
num2 = int(input('Enter num2: '))
print(str(num1) + ' / ' + str(num2) + ' = ' + str(num1 / num2))
break
except:
print('ERROR')
我想要的是程序要对用户说的 "No you did this wrong! Now do it again until you get it right." 我试过在 java 中写这个:
import java.util.Scanner;
public class TryCatch {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
try {
System.out.print("Enter num1: ");
int num1 = input.nextInt();
System.out.print("Enter num2: ");
int num2 = input.nextInt();
System.out.println(num1 + " / " + num2 + " = " + num1 / num2);
break;
} catch (Exception e) {
System.out.println(e);
System.out.println(e.getCause());
System.out.println(e.getMessage());
}
}
}
}
现在,当 num2 等于 0 时,这也正是我想要的。作为参考,这是发生这种情况时的输出,我在第一次迭代后终止了进程:
Enter num1: 5
Enter num2: 0
java.lang.ArithmeticException: / by zero
null
/ by zero
Enter num1:
Process finished with exit code 1
然而,当 num1 或 num2 是字符串时,java 会在无限循环中打印以下 3 行,直到我终止进程:
java.util.InputMismatchException
null
null
这显然意味着我做错了什么,但对于我的一生,我不知道为什么 catch 块将 运行ning 保持在一个无限循环中,但没有 zeroDivision 类型错误错误... 所以我的 2 个具体问题是:
- 如何实现我所描述的我想做的事情?即让程序告诉用户 "No you did this wrong! Now do it again until you get it right."
- 这是一个不正确的解决方案的原因是什么?即,当存在类型错误但没有 zeroDivision 错误时,为什么 catch 块 运行 处于无限循环中?
编辑: 由于我只能接受 1 个答案,我想特别感谢 @rgettman 解释 nextInt() 方法如何导致我的特定问题,并感谢 @Alireza Dastyar 为我提供了我认为更优雅的解决方案,因为它改变了抛出异常的位置from 而不是在事后清理混乱的输入,这意味着当没有抛出特定异常时你已经给出了虚拟输入。
执行永远不会被零除。当您输入 nextInt()
无法处理的内容时,该方法会在执行到达除法表达式之前抛出 InputMismatchException
。
此外,当使用输入的 Scanner
方法抛出该异常时,它不会使用有问题的输入,循环重复,并且 nextInt()
在 相同 输入,导致无限循环。
nextInt()
javadocs:
This method will throw
InputMismatchException
if the next token cannot be translated into a valid int value as described below. If the translation is successful, the scanner advances past the input that matched.
- 在
catch
块中,将消息打印给用户重试。 - 在
catch
块中调用input.next()
以处理不正确的输入并前进到下一个标记。
首次输入错误时,您需要谨慎处理;在这种情况下,可能有 2 个输入标记要跳过。
考虑调用 hasNextInt()
以确定输入中是否有要解析的整数。