2

Is there a built-in way to determine in a finally block whether or not you just came out of a catch block? I know this can easily be done with a variable like below, but I was just curious if there was a standard, built-in way of doing this.

boolean bException = false;
try
{
    dbf = new DBFunctions();
    dbf.db_run_query(query2);
    dbf.rs.next();
    nMonth = dbf.rs.getInt("calquarter") * 3;
    nYear = dbf.rs.getInt("calyear");

}
catch (SQLException sqle)
{
    out.println(sqle.toString());
    bException = true;
}
finally
{
    dbf.closeConnection();
    if (bException == true)
        return;
}

Update: Here are the contents of the closeConnection() method, which is just trying to close all of the database objects:

public void closeConnection()
{

    if (rs != null)
    {
        try { rs.close(); } catch (SQLException e) { ; }
        rs = null;
    }

    if (stmt != null)
    {
        try { stmt.close(); } catch (SQLException e) { ; }
        stmt = null;
    }

    if (con != null)
    {           
        try { con.close(); } catch (SQLException e) { ; }
        con = null;
    }



}

3 Answers 3

8

Not as far as I know. In this case, however, you might be better off by simply putting the return inside the catch block. The finally block will still be run.

Sign up to request clarification or add additional context in comments.

6 Comments

moving the return to the try block will not produce the same behaviour. As it is, the return in the finally is swalling all exceptions. If you move it to the try block, then exceptions are propagated as normal. See weblogs.java.net/blog/staufferjames/archive/2007/06/…
@mdma: I said the catch block, not the try block.
@mdma: Furthermore, since the return in his code is conditional, it will only swallow SQLExceptions.
Yes your right, I overlooked the conditional. Either way, I'd recommend reworking or at least comment this code, since it's not obvious what it does with the exception.
This is in a jsp page. For the exception handling the intention is to just return the exception to the screen. But I want to make sure I close all database objects whether there is an exception or not.
|
4

There is no standard way - the language doesn't distinguish between finally block called as part of normal or abnormal termination of the block.

I've occasionally needed this pattern. Rather than set the flag when an exception occurs, set it at the end of the normal code.

boolean success = false;
try
{
   doStuff();
   success = true;
}
finally
{
   if (!success)
   {
      // there was an exception
   }
}

Then your finally block knows if it's being called from normal or exceptional termination.

EDIT: It's not considered good practice to return in a finally block, since it swallows up exceptions. This is implicit and not clear from the code (or from the language spec!) As written, your finally block will stop the SQLException from being propagated. If that's what you want, then do this with the catch block, to make it explicit. See Returning from a finally block in Java.

1 Comment

+1 - good point, as there is only one try block, but there might be many catch blocks.
2

As far as I know, there is no way to do that. In the first place, the purpose of finally block is to do something that must be done whether there is an exception or not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.