STRANGE BEHAVIOR IN JAVA STATEMENT "TRY_CATCH_FINALLY"

In the following code snippet:
--------------------------------
try {
  Class reflectService = Class.forName(serviceName);
  Class signature[] = new Class[] { Connection.class,  
                       String.class, HashMap.class };
  Method reflectInvoke = reflectService.getMethod
                               ("invoke", signature);
  result = (ArrayList) reflectInvoke.invoke(
                           reflectService,
                           new Object[] { _conn,
                                          lang,
                                          hmParam });
} catch (ClassNotFoundException cnfe) {
  throw new EJBException(cnfe.getException().getMessage(), cnfe);
} catch (NoSuchMethodException nsme) {
  throw new EJBException(nsme.getMessage(), nsme);
} catch (InvocationTargetException ite) {
  throw new EJBException(ite.getTargetException().getMessage(), ite);
} catch (Exception e) {
  throw new EJBException("Message1");

} finally {
  try {
    if (_conn != null) {
      _conn.close();
    }
    _conn = null;
  } catch (Exception e) {
    throw new EJBException("Message2"};
  }
}
-------------------------------------

Why the hell does this execution never reaches the "finally" block? In JDK1.3.1 specification (which I am using) it clearly states that "finally" statements are executed whether a catch block terminates abruptly or not. The thing here is that if the finally block is not reached the connection I'm using for the execution of a query is never closed, so I'm running out of connections in my pool.

I tried writing a close statement before each thrown exception, but that wouldn't be a clean code.

I hope anyone can help.

Best regards.

Alsvartr.
Web Developer.
jramos@legosoft.com.mx
Legosoft, S.C.
Mexico City.
alsvartrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
What happens when you put a System.out.println immediately after each try opening brace?
0
socrateskCommented:
Try putting some 'System.out.println()' statements in 'finally' block and check. Because, there is no way the 'finally' block not getting executed in the above snippet.

Also may check your called method. Whether that method calls 'System.exit(0)' or something like that. In that case, the 'finally' block will not be called at all.

Btw, setting 'conn=null;' is not required as we close the connection in finally block.

Socrates
Chennai-India.
0
ayeenCommented:
chances are the conditions (the "if" part) inside your "finally" clause wasn't satisfied... that's why none of the statements inside your "finally" was executed.....

try tracing your codes (see socratesk reply) and see what "_conn" contains.....

System.out.println("inside finally: " + _conn);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alsvartrAuthor Commented:
Quite right about that!
0
CEHJCommented:
>>Why the hell does this execution never reaches the "finally" block?

was your question

>>chances are the conditions (the "if" part) inside your "finally" clause wasn't satisfied

is the answer to a different question. The remedy offered:


>>try tracing your codes (see socratesk reply) and see what "_conn" contains.....

was the THIRD time it was offered, so i consider you have awarded the points to the wrong person alsvartr
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.