eclipse warning: Finally block doesn't complete normally
-
01-07-2021 - |
Question
I have some code that throws a potential error and I catch these, then jump into a finally
block, here it is in 'pseudo' form.
private boolean myMethod(ResultSet rs1, ResultSet rs2){
Vector<String> temp = new Vector<String>();
try{
//add info into temp while condition is true
while(true){temp.add(information);}//potentially throws an SQL error
//test my temo object for some codition
if(false){throw InternalError();}
}catch (SQLException e){
//send error message
}
catch (InternalError e){
//send error message
}
finally{
//whatever happens I need to send a result of some sort!
if(temp.size() > 1){
//create class member variable from this info
}
//send a message explaining that an error has occurred.
//this is the message to say if things have worked or not, so as the
//calling code can handle an re-run this method until this value returns true.
return booleanValue
}//end finally
}//end method
I'm not sure exactly what this error message means in this instance.
I generally handle any exceptions within any method that throws them, should I in this instance throw rather than handle the Exception, and catch both of the errors in a custom Exception?
The first 2 messages that I send out are mainly for development purposes, and because I have to handle them. The message in my finally block will ultimately comprise the message to the user, asking them to confirm the situation, make a modification, abort or change how we got to this situation.
What should I do to 'remove' the compiler message. I don't like to suppress warnings in my code as I don't like the implication that I may suppress something else further down the chain, and also the suppression is for the whole method, I can't add it in for just this small block of code.
Is there any advice for my situation.
Thanks in advance.
edit1: To reflect comments, sorry forgot to put the correct return type (silly mistake) same this the if(test){} closing brace.
PS. To those that have 'downvoted' you could put a comment and give me a chance to edit prior to downvoting. thanks. Also in my code these things aren't giving me problems, its a case of 'a quertyscript malfunction' between brain and fingers during posting.
Solution
Method return type is void
and you have return statement return booleanValue
in your finally block
. I would use finally block to close non-java resource.
If you want to return values then either return it in try block or catch block OR outside of try{}
catch{}
but not in finally{}
.
OTHER TIPS
break, continue *return* and throw are abrupt completions of statements. You shoud do the return outside the finally block. Additionally you are returning in void method.
If you had formatted your code, you would have seen the finally
clause is inside a catch
, and not linked to any try
clause.
You should have
try {
} catch {
} finally {
}
and not
try {
} catch {
finally {
}
}
This is because you have return statement in finally block, which is not advisable and considered a bad coding style.