简体   繁体   English

Java终于阻止了问题

[英]Java finally block question

Is there a built-in way to determine in a finally block whether or not you just came out of a catch block? 是否有一种内置方法可以在finally块中确定您是否刚刚从catch块中出来? 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: 更新:以下是closeConnection()方法的内容,它只是试图关闭所有数据库对象:

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



}

Not as far as I know. 不是我所知道的。 In this case, however, you might be better off by simply putting the return inside the catch block. 但是,在这种情况下,只需将return放在catch块中就可能会更好。 The finally block will still be run. finally块仍将运行。

There is no standard way - the language doesn't distinguish between finally block called as part of normal or abnormal termination of the block. 没有标准的方法 - 语言不区分finally块被称为块的正常或异常终止的一部分。

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. 然后你的finally块知道它是从正常终止还是异常终止调用。

EDIT: It's not considered good practice to return in a finally block, since it swallows up exceptions. 编辑:在finally块中返回并不是一种好习惯,因为它会吞没异常。 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. 这是隐含的,并且不清楚代码(或语言规范!)。如上所述,finally块将阻止SQLException传播。 If that's what you want, then do this with the catch block, to make it explicit. 如果这是您想要的,那么使用catch块执行此操作,以使其明确。 See Returning from a finally block in Java . 请参阅从Java中的finally块返回

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. 首先, finally块的目的是做一些必须要做的事情,无论是否存在异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM