Java 达不到语句

Java 达不到语句,java,Java,我做了一个方法,目的是删除问题列表。方法测试包括问题、答案、问题数量和分数。而且效果很好 我得到以下错误: 无法访问的语句:System.out.println(“The test\”+tests[indice-1].getNomTest()) 代码如下: public static int supprimerTest(Test[] tests, int nbrTests) { int longueurTests = tests.length; int indice = 0;

我做了一个方法,目的是删除问题列表。方法测试包括问题、答案、问题数量和分数。而且效果很好

我得到以下错误:

无法访问的语句:System.out.println(“The test\”+tests[indice-1].getNomTest())

代码如下:

public static int supprimerTest(Test[] tests, int nbrTests) {

    int longueurTests = tests.length;
    int indice = 0;
    int noTest = 1;
    int saisieNoTest = 0;
    String nomTest;        



    System.out.println("***DELETE A TEST***\n");

    if (nbrTests > 0) {

        boolean fin = true;

        do{

            System.out.print("Please enter a number of the question to be deleted");

            try {
               indice = Clavier.lireInt();
                if (indice < 1 || indice > nbrTests){

                    throw new IndexOutOfBoundsException();

                    System.out.println("The test \"" + tests[indice - 1].getNomTest());
                    tests[indice-1] =null;
                    nbrTests--;
                    fin = false;
                }

            }catch (Exception e) {
                if (nbrTests < 1){
                    System.out.print("ERROR ! the number must be between 1 and " + nbrTests + "try again...");
                }else {
                    System.out.println("ERROR ! the number must 1. ... Try again...");

                }
            }
        }while (fin);

    }else {
        System.out.println("Il n'existe aucun test.");
        System.out.print ("\nTPress <ENTRER> to continue ...");
        Clavier.lireFinLigne();

    }

    return nbrTests; 
}
公共静态int-suppliertest(Test[]tests,int-tests){
int longueurTests=测试长度;
int-indice=0;
int noTest=1;
int saisieNoTest=0;
串试验;
System.out.println(“***删除测试***\n”);
如果(NBR测试>0){
布尔值=真;
做{
系统输出打印(“请输入要删除的问题的编号”);
试一试{
indice=Clavier.lireInt();
如果(指示<1 | |指示>NBR测试){
抛出新的IndexOutOfBoundsException();
System.out.println(“测试\”+测试[indice-1].getNomTest());
测试[indice-1]=空;
测试--;
fin=假;
}
}捕获(例外e){
如果(NBR试验<1){
System.out.print(“错误!数字必须介于1和“+1+”之间,请重试…”);
}否则{
System.out.println(“错误!号码必须为1…请重试…”);
}
}
}while(fin);
}否则{
System.out.println(“Il n'existe aucun test”);
System.out.print(“\n按继续…”);
键盘。里弗林林();
}
返回测试;
}

谢谢您的帮助。

出现此错误的原因是,异常的行为类似于返回语句,它将被最近的异常处理程序捕获

既然你有:

throw new IndexOutOfBoundsException();
该抛出下的任何代码都将永远无法到达,因为它会立即跳转到您的catch块


我希望这是有道理的

当您抛出异常时,将不会执行抛出下面的代码。抛出invoke异常,该方法只能在catch/finally块中继续。
之后的行抛出新的IndexOutOfBoundsException()。也许您的代码应该如下所示:

if (indice < 1 || indice > nbrTests){
     throw new IndexOutOfBoundsException();
}
System.out.println("The test \"" + tests[indice - 1].getNomTest());
tests[indice-1] =null;
nbrTests--;
fin = false;
if(指示<1 | |指示>测试){
抛出新的IndexOutOfBoundsException();
}
System.out.println(“测试\”+测试[indice-1].getNomTest());
测试[indice-1]=空;
测试--;
fin=假;

当您使用try语句时,如果检测到异常,它会自动抛出异常。因此,只需取出抛出异常行,您的代码就会正常工作。

您了解抛出的功能吗?在这种情况下,请重新考虑,您就得到了答案。如果不了解,请仔细阅读。可能重复的