2013-04-23 89 views
0

我试图显示错误消息时,该指数是出界,但是当有数组列表中没有的元素我当前的代码甚至没有显示相应的错误信息。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; 
} 
+1

“ArrayList”,显然是**空**,您试图得到'IndexOutOfBoundsException'? – SudoRahul 2013-04-23 10:48:44

+0

问题是第二个catch方法不显示错误消息。数组列表是分开的。我只是想让异常显示错误消息而不是崩溃。 – user2290426 2013-04-23 10:51:18

+0

@ user2290426:您发布的代码中没有任何内容会生成该异常。 (除非'displayText.getText()'有,但这将是一个奇怪的接口。) – Mat 2013-04-23 10:52:05

回答

0

如果需要检查的大小,那么你可以调用size()方法,你也可以抛出异常所以MakeCall函数方法可以决定作用:

public void makeCall() { 
    try { 
     Mobile phoneCall = (Mobile) gadgets.get(getDisplay()); 
     phoneCall.PhoneCall(getPhoneNumber(), getDuration()); 
     System.out.println(); 
    } 
    catch(NumberFormatException exception) { 
     JOptionPane.showMessageDialog(frame, "Please enter a positive Display"); 
    } 
    catch(IndexOutOfBoundsException exception) { 
     JOptionPane.showMessageDialog(frame, "Gadget is not listed"); 
    } 
    catch(IllegalArgumentException exception) { 
     JOptionPane.showMessageDialog(frame, "Please enter a positive Display"); 
    } 
} 

public int getDisplay() throws NumberFormatException 
{ 
    int gadgetDisplay = Integer.parseInt(displayText.getText()); 

    if (gadgetDisplay < 0) { 
     throw new IllegalArgumentException("Please enter a positive Display"); 
    } 
    else if (gadgetDisplay >= yourList.size()) { 
     throw new IndexOutOfBoundsException("Gadget is not listed"); 
    } 

    return gadgetDisplay; 
} 

,或者你可以在一个方法做到这一点:

public void makeCall() { 
    try { 
     int gadgetDisplay = Integer.parseInt(displayText.getText()); 

     if (gadgetDisplay < 0) { 
      JOptionPane.showMessageDialog(frame, "Please enter a positive Display"); 
     } 
     else if (gadgetDisplay >= yourList.size()) { 
      JOptionPane.showMessageDialog(frame, "Gadget is not listed"); 
     } 
     else { 
      Mobile phoneCall = (Mobile) gadgets.get(getDisplay()); 
      phoneCall.PhoneCall(getPhoneNumber(), getDuration()); 
      System.out.println(); 
     } 
    } 
    catch(NumberFormatException exception) { 
     JOptionPane.showMessageDialog(frame, "Please enter a positive Display"); 
    } 
} 
0

你的第二个catch块才会执行。

你的try块中没有任何东西会抛出异常。正如你所说ArrayList在其他地方被处理,你很可能需要把试图访问ArrayList的元素的代码放在它自己的try catch块中。该例外需要在那里处理。

说实话,我认为你已经贴错了一段代码在这里。

+0

感谢您的信息。数组列表在其他地方被实现,但你是正确的,我必须访问方法内部的元素才能工作。 – user2290426 2013-04-23 11:05:50

0

您不会产生期望的异常,因为您并未尝试执行某些操作,因此会发生异常。

OutOfBound异常意味着你正试图从获得的值超出范围

public int getDisplay() 
{ 
    int gadgetDisplay = 0; 
    ArrayList arr = new ArrayList(); 
    arr.add("a"); 
    try 
    { 
     String str = arr.get(3); 

    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中

+0

你确定吗?它会抛出'ArrayIndexOutOfBoundsException',而不是'IndexOutOfBoundsException'。在那里使用'ArrayList'。 – SudoRahul 2013-04-23 11:02:56

+0

谢谢。我必须访问方法内的数组列表才能工作。我现在有它的功能。 – user2290426 2013-04-23 11:07:20

0

你不应该使用try/catch块来做到这一点期待例外 - 你已经知道有多少元素的ArrayList中因此就使用size(),以确保数在范围内。