0

I made a method which purpose is to delete list of questions. The method Test contains questions, answers, number of questions, points. And works fine.

I get the following error:

Unreachable statement on : System.out.println("The test \"" + tests[indice - 1].getNomTest());

Here is the code:

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; 
}

Thank you for your help.

zero323
  • 322,348
  • 103
  • 959
  • 935
May5
  • 1
  • 2
  • 1
    Do you understand what `throw` does? In that case, think about it again and you have the answer. If not, read it up. – Polygnome Apr 14 '18 at 23:42
  • Possible duplicate of [The throws keyword for exceptions in Java](https://stackoverflow.com/questions/1989077/the-throws-keyword-for-exceptions-in-java) – Polygnome Apr 14 '18 at 23:42

3 Answers3

0

The reason you have that error is because exceptions act similar to a return statement where it'll get caught by the nearest Exception handler.

Since you have:

throw new IndexOutOfBoundsException();

Any code underneath that throw will never be reached because it immediately jumps to your catch block.

I hope that makes sense. :)

0

When you throw an exception, the code below the throw will be not executed. Throw invoke exception and the method can continue only in catch/finally block. Lines after throw new IndexOutOfBoundsException(); cannot be reached. Maybe your code should be following:

if (indice < 1 || indice > nbrTests){
     throw new IndexOutOfBoundsException();
}
System.out.println("The test \"" + tests[indice - 1].getNomTest());
tests[indice-1] =null;
nbrTests--;
fin = false;
Ján Яabčan
  • 743
  • 2
  • 10
  • 20
0

When you use try statement, it throws exceptions automatically if it is being detected. Therefore, simply take out the throw exception line, then your code should work.