Reputation: 523
I am getting underline in red color at my catch statement, not sure what is wrong the error that I am getting is 'never thrown in body of a corresponding try statement' what does it mean? everything looks fine to me.
public void validateTriangle(int sidea, int sideb, int sidec) throws InvalidValueException {
try {
if ((sidea + sideb > sidec) || (sideb + sidec > sidea) || (sidea + sidec > sideb)) {
findArea(side1, side2, side3);
}
} catch (InvalidValueException excep) {
message = excep.getMessage();
}
}
Upvotes: 0
Views: 186
Reputation: 1665
Check that findArea
explicitly throws InvalidValueException
... findArea(...) throws InvalidValueException {
...
}
Upvotes: 0
Reputation: 8158
You're getting this red line / compile-time error as your method
throws InvalidValueException
This means your method doesn't handle this exception & it will be passed on to the caller method. So in this case catch block is never going to be executed.
Upvotes: 0
Reputation: 4568
The code that runs inside the try block never will throw an InvalidValueException
. That is what he is telling you.
The catch block is unnecessary
Upvotes: 1
Reputation: 1385
Your try-catch
overrides your throws InvalidValueException
You shoud remove your try-catch
, or remove your throws InvalidValueException
.
Upvotes: -2
Reputation: 1073978
"never thrown in body of a corresponding try statement" means exactly what it says: You've told the code to catch an InvalidValueException
, but it's not thrown by any code within the try
block. Apparently findArea
doesn't throw that exception (and obviously your inline code doesn't).
A try/catch
block is used to catch exceptions that occur within the try
block and handle them (in the corresponding catch
block), so there's no point in a catch
for an exception that never occurs in the try
. The IDE (and I believe the lint features of the java
compiler tool) will flag that up for you on the basis that catching an error that isn't going to happen is probably a logic error.
Upvotes: 6