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;
    }



}
opike
  • 7,053
  • 14
  • 68
  • 95

3 Answers3

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.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
  • 2
    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 http://weblogs.java.net/blog/staufferjames/archive/2007/06/_dont_return_in.html – mdma Apr 16 '11 at 15:35
  • @mdma: I said the `catch` block, not the `try` block. – Aasmund Eldhuset Apr 16 '11 at 15:39
  • @mdma: Furthermore, since the `return` in his code is conditional, it will only swallow `SQLException`s. – Aasmund Eldhuset Apr 16 '11 at 15:40
  • 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. – mdma Apr 16 '11 at 15:42
  • 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. – opike Apr 17 '11 at 03:36
  • @opike: That is reasonable, and your code does precisely that. My point was just that you can achieve the exact same effect with simply `catch (SQLException sqle) { out.println(sqle.toString()); return; } finally { dbf.closeConnection(); }`. – Aasmund Eldhuset Apr 17 '11 at 19:34
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.

Community
  • 1
  • 1
mdma
  • 56,943
  • 12
  • 94
  • 128
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.

Hery
  • 7,443
  • 9
  • 36
  • 41