我试图显示错误消息时,该指数是出界,但是当有数组列表中没有的元素我当前的代码甚至没有显示相应的错误信息。IndexOutOfBoundsException异常不工作
这里是为部分我想实现到异常的代码块。 来解释,make调用方法获取与数组元素相对应的数字的显示,然后显示来自正确索引的调用。如果在你的try块中的代码引发IndexOutOfBoundsException
public void makeCall()
{
Mobile phoneCall = (Mobile) gadgets.get(getDisplay());
phoneCall.PhoneCall(getPhoneNumber(), getDuration());
System.out.println();
}
public int getDisplay()
{
int gadgetDisplay = 0;
try
{
gadgetDisplay = Integer.parseInt(displayText.getText());
if (gadgetDisplay< 0)
{
JOptionPane.showMessageDialog
(frame, "Please enter a positive Display");
}
}
catch(NumberFormatException exception)
{
JOptionPane.showMessageDialog
(frame, "Please enter a positive Display");
}
catch(IndexOutOfBoundsException exception)
{
JOptionPane.showMessageDialog
(frame, "Gadget is not listed");
}
return gadgetDisplay;
}
“ArrayList”,显然是**空**,您试图得到'IndexOutOfBoundsException'? – SudoRahul 2013-04-23 10:48:44
问题是第二个catch方法不显示错误消息。数组列表是分开的。我只是想让异常显示错误消息而不是崩溃。 – user2290426 2013-04-23 10:51:18
@ user2290426:您发布的代码中没有任何内容会生成该异常。 (除非'displayText.getText()'有,但这将是一个奇怪的接口。) – Mat 2013-04-23 10:52:05