Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » Compiler warning about finally
Compiler warning about finally [message #279838] Mon, 24 January 2005 16:28 Go to next message
Eclipse UserFriend
Originally posted by: rhino1.NOSPAM.sympatico.ca

I've got a compiler warning that has me quite baffled.

Here is my code:

if (sqlCode != 0 || !sqlState.equals("00000")) {
System.err.println("Stored procedure " + STORED_PROCEDURE_NAME + "
failed.");
System.err.println("SQLState = " + sqlState);
System.err.println("SQLCode = " + sqlCode);
System.err.println("SQLMessage = " + sqlMessage);
System.err.println("ProcMessage = " + procMessage);
System.err.println("StackTrace:\n" + stackTrace);
try {
conn01.rollback();
}
catch(SQLException sql_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " - Encountered error
while " +
"attempting to roll back.");
sql_excp.printStackTrace();
}
finally {
return;
}
}

and here is the warning:

finally block does not complete normally


I don't have much experience with finally but, as far as I can tell, I'm not
doing anything odd here. If I get an error from my database call, I want to
print some diagnostics and attempt to rollback the transaction, then return
from the method. If the rollback fails, I want to print some additional
diagnostics and, again, return from the method.

Is there a problem with doing a return in a finally block? If there is some
sort of problem with my approach, can anyone suggest an alternative way of
accomplishing the same thing?

I'm running Eclipse 3.0.1 on Windows XP.

--
Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare
Re: Compiler warning about finally [message #279842 is a reply to message #279838] Mon, 24 January 2005 16:36 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: olivier_thomannNOSPAM.ca.ibm.com

Rhino a écrit :
> Is there a problem with doing a return in a finally block? If there is some
> sort of problem with my approach, can anyone suggest an alternative way of
> accomplishing the same thing?
Putting a return statement in a finally block is not a good idea :-).
Doing this, you silently ignore any exception that could happen in your
code. Let's say, you got a NullPointerException executing your
conn01.rollback();, with your code, you would never know that you had a
NullPointerException. A finally block is executed whatever the execution
path was, but if you return from the finally block, you won't get an
uncaught exception/error rethrown.
You should never put a return or a throw statement inside a finally
block. When you do that, the finally block "doesn't complete normally".

In your example, I wonder why you need a finally block. Simply put a
return statement.
if (sqlCode != 0 || !sqlState.equals("00000")) {
System.err.println("Stored procedure " + STORED_PROCEDURE_NAME + "
failed.");
System.err.println("SQLState = " + sqlState);
System.err.println("SQLCode = " + sqlCode);
System.err.println("SQLMessage = " + sqlMessage);
System.err.println("ProcMessage = " + procMessage);
System.err.println("StackTrace:\n" + stackTrace);
try {
conn01.rollback();
}
catch(SQLException sql_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " - Encountered
error
while " +
"attempting to roll back.");
sql_excp.printStackTrace();
}
return;
}

In case of error or runtime exception inside the try block or the catch
block, you would be able to get a stack trace.

Hope this help,
--
Olivier
Re: Compiler warning about finally [message #279850 is a reply to message #279842] Mon, 24 January 2005 17:41 Go to previous message
Eclipse UserFriend
Originally posted by: rhino1.NOSPAM.sympatico.ca

"Olivier Thomann" <olivier_thomannNOSPAM@ca.ibm.com> wrote in message
news:ct3ppj$l68$1@www.eclipse.org...
> Rhino a
Previous Topic:[Howto] create a new SearchPage etc. in 3.0 (eight or ten questions not answered anywhere)
Next Topic:How to uninstall features and plugins from an install handler
Goto Forum:
  


Current Time: Tue Jul 22 00:55:28 EDT 2025

Powered by FUDForum. Page generated in 0.06674 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top