Java 方法的运行时异常

Java 方法的运行时异常,java,Java,我已经基本完成了代码的编写,所有的测试用例都给出了正确的结果,只有两个例外,其中一个数组为空,应该抛出一个运行时异常,消息为empty array has no mode。我的测试失败表明:空输入应该生成一个RuntimeException,消息为Empty array has no mode。我已经在代码中包含了这一点,但仍然没有通过这些测试用例。其中一个测试用例是: @Test(timeout=2000) public void mode_empty1(){ String msg;

我已经基本完成了代码的编写,所有的测试用例都给出了正确的结果,只有两个例外,其中一个数组为空,应该抛出一个运行时异常,消息为empty array has no mode。我的测试失败表明:空输入应该生成一个RuntimeException,消息为Empty array has no mode。我已经在代码中包含了这一点,但仍然没有通过这些测试用例。其中一个测试用例是:

@Test(timeout=2000) public void mode_empty1(){
    String msg;
    String expect = "Empty array has no mode";
    try{
      Mode.mode(new String[]{});
    }
    catch(RuntimeException e){
      String actual = e.getMessage();
      msg = String.format("\nRuntimeException has wrong message\nExpect: %s\nActual: %s\n",
                          expect,actual);
      assertEquals(msg,expect,actual);
      return;
    }
    msg = String.format("\nEmpty input should yield a RuntimException with message '%s'\n",
                        expect);
    fail(msg);
  }
能帮我吗

public class Mode {

    public static <T> Pair<T, Integer> mode(T items[])
    {
        try
            {

            if(items == null) throw new RuntimeException("Empty array has no mode");

            else
            {
                T element = items[0];
                int count = 0;

                for(int i = 0; i < items.length; i++)
                {
                    int tempCount = 0;
                    T tempElement = items[i];

                    for(int j = 0; j < items.length; j++)
                        {
                            if(tempElement.equals(items[j]))
                                tempCount++;
                        }   
                    if(tempCount > count)
                    {
                        count = tempCount;
                        element = tempElement;
                    }
                }
                return new Pair<T, Integer>(element, new Integer(count));
            }


        }

        catch(RuntimeException e)
        {
            System.out.println(e.getMessage()); 

        }


    return null;
}
}
条件ifitems==null测试数组是否为null,而不是数组是否为空。但是当您执行Mode.modenew String[]{}时,会传递一个空数组,而不是null

您可以通过执行以下操作来检查空数组:

if(items == null || items.length == 0) throw new RuntimeException("Empty array has no mode");
或将空值传递给Mode.Mode:

由于您希望在模式之外捕获RuntimeException,因此不再需要try-catch块:

public static <T> Pair<T, Integer> mode(T items[]) {
    if (items == null || items.length == 0) {
        throw new RuntimeException("Empty array has no mode");
    } else {
        T element = items[0];
        int count = 0;

        for(int i = 0; i < items.length; i++) {
            int tempCount = 0;
            T tempElement = items[i];

            for(int j = 0; j < items.length; j++) {
                if (tempElement.equals(items[j])) {
                    tempCount++;
                }
            }   
            if (tempCount > count) {
                count = tempCount;
                element = tempElement;
            }
        }
        return new Pair<T, Integer>(element, new Integer(count));
    }
    return null;
}

生产代码检查空输入,但测试代码通过非空输入

您应该更改测试代码以传入空输入:

Mode.mode(null);
或将生产代码中的条件更改为:

if(items.length == 0) throw ...
您可能希望检查生产代码中的空值和空值:

if(items == null || items.length == 0) throw ...
您还需要在try块之前移动此检查,因为您将立即捕获抛出的异常

if(items == null) throw new RuntimeException("Empty array has no mode");
try {
  ...
} catch (RuntimeException e) {
  ...
}

如果传入null而不是新字符串[]{},或者检查items.length==0而不是/以及items==null,则会出现异常。我以前就是这样做的,ifitems==null | | items.length==0。。还是不行,安德森!我得到了它!顺便说一句,如果条件不好,我搬家后真的需要试一试的方法吗?很乐意帮忙!我认为删除try-catch块应该可以。哦,在try块之前移动支票。您正在捕获RuntimeException,因此该异常永远不会从抛出它的方法中消失。将检查移出是什么意思??
if(items == null) throw new RuntimeException("Empty array has no mode");
try {
  ...
} catch (RuntimeException e) {
  ...
}