vendredi 2 février 2018

Java: Exception is never thrown in the corresponding try block

I have this method

public int addInt(int x, int y){
try{
    if(x<1 || y<1){
    throw new InvalidValueExeption();
    }
}
catch(InvalidValueExeption i){
    System.out.println(i);
}
return x+y;
}

InvalidValueExeption is a custom exception. So I wanted to test this:

@Test
public void test(){
AddClass a = new AddClass();
    boolean thrown = false;

try{
    a.addInt(-2, 3);
}
catch(InvalidValueException e){
    thrown=true;
}

assertTrue(thrown);
}

I can't run this test, because it says Exception exception.InvalidValueException is never thrown in the corresponding try block. What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire