Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

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.
0
alsvartr
Asked:
alsvartr
1 Solution
 
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
 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now