-3

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.

zx81
  • 41,100
  • 9
  • 89
  • 105
DaveM
  • 734
  • 4
  • 11
  • 35

4 Answers4

7

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.

Desislav Kamenov
  • 1,193
  • 6
  • 13
5

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 {

    }
}
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
4

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{}.

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
  • if I put a return statement in my catch, then surely I will never get to the finaly as the method has already returned in one or other of my catch statements? – DaveM Sep 10 '12 at 08:31
  • 1
    You will **always** get to the `finally`! – f_puras Sep 10 '12 at 08:54
  • I just searched for 'does finaly always run after return' and found this on SO: http://stackoverflow.com/questions/65035/in-java-does-return-trump-finally whick answers my question. I'm going to mark this as the answer and give +1 to other who had mentioned a similar thing. – DaveM Sep 10 '12 at 09:09
4

This is because you have return statement in finally block, which is not advisable and considered a bad coding style.