我正在为我的 java 类开发一个计算器项目,该项目要求我使用异常。当在计算器中输入运算符后跟数字以外的任何内容时,它应该捕获 NumberFormatException 并显示一条错误消息,指出“无效的非数字操作数”。相反,它显示了不同的错误。
这是我必须捕获错误的代码:
catch (NumberFormatException e)
{
System.out.println(e.getMessage());
input = kb.nextLine();
}
我有这个来显示消息:
public class NumberFormatException extends IllegalArgumentException
{
public NumberFormatException()
{
super("Invalid non-numeric operand.");
}
}
这是我得到的错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Calculator.doCalculation(Calculator.java:59)
at CalculatorTest.main(CalculatorTest.java:25)
但是我不确定从这里开始做什么,因为我不太明白这个异常。如果这很简单,我很抱歉,我对java很陌生。请帮忙,谢谢!
最佳答案
您的异常是 ArrayIndexOutOfBoundsException 。这与字符串的格式无关。它是关于尝试访问不存在的数组元素。
查看第 59 行,如消息所示。您尚未与我们分享此信息,因此您只能靠自己了。
关于java - 捕获编号格式异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59163529/