?
Solved

STRANGE BEHAVIOR IN JAVA STATEMENT "TRY_CATCH_FINALLY"

Posted on 2003-02-19
5
Medium Priority
?
219 Views
Last Modified: 2010-03-31
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
Comment
Question by:alsvartr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 7984543
What happens when you put a System.out.println immediately after each try opening brace?
0
 
LVL 1

Expert Comment

by:socratesk
ID: 7985528
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
 
LVL 1

Accepted Solution

by:
ayeen earned 600 total points
ID: 7985997
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
 

Author Comment

by:alsvartr
ID: 8018855
Quite right about that!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8019063
>>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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question