Reputation: 39
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?
Upvotes: 2
Views: 14816
Reputation: 7290
Your addInt()
method doesn't throw InvalidValueException
(*). Inside the method, you do throw it, but you catch it before it can "leave" your method. So, for the outside world, there is no InvalidValueException
coming from your method.
Then, correctly the compiler tells you that there's no point in catching the InvalidValueException
.
So, instead of immediately catching the exception inside your method, declare the method to throw InvalidValueException
:
public int addInt(int x, int y) throws InvalidValueException {
if (x < 1 || y < 1) {
throw new InvalidValueException();
}
return x + y;
}
Rationale:
Exceptions are meant to tell the caller (**) of some method that it couldn't fulfill its task, i.e. your addInt()
method is designed to add only positive numbers. And if someone tries it with a number below 1, the method answers with the exception instead of returning a value, thus saying: "Hey, something went wrong, so I can't give you an answer (and the problem description is [the exception with its message and so on])."
( * ) I assume, the missing "c" is just a typo, and you don't have two different exception classes.
( ** ) That's important. I'm not talking about System.out.println()
, as that's telling something to the user, not the caller.
Upvotes: 3
Reputation: 2220
First of i think your InvalidValueExeption
is a subtype of RuntimeException.
RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.
So if you need to indicate that you throw an InvalidValueExeption or inherit Exception
instead.
Here the exception is declared on your method and thrown :
public int addInt(int x, int y) throws InvalidValueExeption {
try {
//..
throw new InvalidValueExeption();
} catch (InvalidValueExeption e) {
// do some logging the throw back at you
throw e;
}
}
Upvotes: 0
Reputation: 47895
If InvalidValueExeption
is a checked exception then the compiler will complain because addInt
is not declared to throw InvalidValueExeption
.
If InvalidValueExeption
is not a checked exception then the test will fail because addInt
swallows the InvalidValueExeption
.
There's also a possible typo in your question: addInt()
throws InvalidValueExeption
whereas test()
tries to catch InvalidValueException
. In the former case exception is spelled "Exeption", in the latter case it is spelled "Exception", note the missing "c".
The following approach will work:
public int addInt(int x, int y) {
if (x < 1 || y < 1) {
throw new InvalidValueException();
}
return x + y;
}
@Test(expected = InvalidValueException.class)
public void test(){
AddClass a = new AddClass();
a.addInt(-2, 3);
}
Upvotes: 1