I have a method, that connects to mail server, gets all the messages and returns these messages in an Array.
So this looks like this (pseudocode):
public Message[] getMessages() throws Exception {
try{
//Connection to mail server, getting all messages and putting them to an array
return Message[];
} finally {
CloseConnectionToMailServer(); //I don't need it anymore, I just need messages
}
}
I can put “return” instruction to “finally” block, but this disable potential Exception.
If I leave it as it is now, “return” can never be reached.
I think you caught the problem I ran at. How can I get all the messages I need, return an array with these messages and close connection to server in a delicate (in even “best practices”) way?
Thank you in advance.
Your method is just fine. Even if you return from a try block finally block will be executed.
And your method must return a value :