[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.platform] Re: Compiler warning about finally

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