Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java “如何修复”;未能抛出“异常”;在爪哇_Java_Exception - Fatal编程技术网

Java “如何修复”;未能抛出“异常”;在爪哇

Java “如何修复”;未能抛出“异常”;在爪哇,java,exception,Java,Exception,我试图编写一个程序,对两个整数的列表进行排序,使元素按升序排列,列表的大小保持不变,如果列表的大小不等于2,则给出一个IllegalArgumentException 这就是我所拥有的: public static void sort2(List<Integer> t) { //t is a the list if(t.size() < 3) { Collections.sort(t); System.out.pr

我试图编写一个程序,对两个整数的列表进行排序,使元素按升序排列,列表的大小保持不变,如果列表的大小不等于2,则给出一个IllegalArgumentException

这就是我所拥有的:

public static void sort2(List<Integer> t) { //t is a the list
        if(t.size() < 3) {
            Collections.sort(t);
            System.out.println(t);
        }
        else if (t.size() == 2) {
            throw new IllegalArgumentException("List is empty");
        }
}
publicstaticvoidsort2(List t){//t是列表中的
如果(t.尺寸()<3){
集合。排序(t);
系统输出打印ln(t);
}
否则如果(t.size()==2){
抛出新的IllegalArgumentException(“列表为空”);
}
}
但是,当我进行JUnit测试时,排序通过,但异常测试失败。测试如下:

public void test10a_sort2() {
    ArrayList<Integer> t = new ArrayList<>();
    String error = "lab0.sort2(t) failed to throw an IllegalArgumentException";
    try {
      lab0.sort2(t);
      fail(error);
    }
    catch (IllegalArgumentException x) {
      // do nothing
    }
    catch (Exception x) {
      fail("lab0.sort2(t) threw the wrong kind of exception");
    }

    t.add(1);
    try {
      lab0.sort2(t);
      fail(error);
    }
    catch (IllegalArgumentException x) {
      // do nothing
    }
    catch (Exception x) {
      fail("lab0.sort2(t) threw the wrong kind of exception" + x);
    }

    t.add(2);
    t.add(3);
    try {
      lab0.sort2(t);
      fail(error);
    }
    catch (IllegalArgumentException x) {
      // do nothing
    }
    catch (Exception x) {
      fail("lab0.sort2(t) threw the wrong kind of exception");
    }
  }
public void test10a_sort2(){
ArrayList t=新的ArrayList();
String error=“lab0.sort2(t)未能抛出IllegalArgumentException”;
试一试{
lab0.sort2(t);
失败(错误);
}
捕获(IllegalArgumentException x){
//无所事事
}
捕获(异常x){
失败(“lab0.sort2(t)抛出了错误类型的异常”);
}
t、 增加(1);
试一试{
lab0.sort2(t);
失败(错误);
}
捕获(IllegalArgumentException x){
//无所事事
}
捕获(异常x){
失败(“lab0.sort2(t)抛出了错误类型的异常”+x);
}
t、 增加(2);
t、 增加(3);
试一试{
lab0.sort2(t);
失败(错误);
}
捕获(IllegalArgumentException x){
//无所事事
}
捕获(异常x){
失败(“lab0.sort2(t)抛出了错误类型的异常”);
}
}
我不确定我做错了什么。我也尝试过使用try-catch,但似乎效果不太好

    if(t.size() < 3) {
        // ...
    }
    else if (t.size() == 2) {
        throw new IllegalArgumentException("List is empty");
    }

然后,如果列表的大小不等于2,则将要执行的内容放在后面。

您的描述中说“如果列表的大小不等于2,则为IllegalArgumentException”。查看您在方法中检查的条件。它正在执行所描述的操作吗?请给我们您的异常日志或告诉我们在哪一行引发了错误。你有几个非法辩论的例外。。。
if (t.size() != 2) {
  throw ...
}