【问题标题】:'finally block does not complete normally' Eclipse warning'finally 块没有正常完成' Eclipse 警告
【发布时间】:2013-07-03 02:33:08
【问题描述】:

Eclipse 在以下代码中向我发出警告:

public int getTicket(int lotteryId, String player) {
    try {
        c = DriverManager.getConnection("jdbc:mysql://" + this.hostname + ":" + this.port + "/" + this.database, this.user, this.password); 
        int ticketNumber;

        PreparedStatement p = c.prepareStatement(
                "SELECT max(num_ticket) " +
                "FROM loteria_tickets " +
                "WHERE id_loteria = ?"
                );
        p.setInt(1, lotteryId);
        ResultSet rs = p.executeQuery();
        if (rs.next()) {
            ticketNumber = rs.getInt(1);
        } else {
            ticketNumber = -1;
        }

        ticketNumber++;

        p = c.prepareStatement(
                "INSERT INTO loteria_tickets " +
                "VALUES (?,?,?,?)");
        p.setInt(1, lotteryId);
        p.setInt(2, ticketNumber);
        p.setString(3, player);
        p.setDate(4, new java.sql.Date((new java.util.Date()).getTime()));
        p.executeUpdate();

        return ticketNumber;
    } catch (Exception e) {
        e.printStackTrace();
    } finally { 
        if (c != null) {
            try {
                c.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return -1;
    }
}

我的代码有什么问题?

【问题讨论】:

  • 移除 return -1 ;应该没问题
  • @Dennis 'finally block 没有正常完成'他在主题行中写了这个
  • 在 try-catch-finally 块之后添加 return -1。

标签: java try-catch-finally


【解决方案1】:

从中删除 return 语句。 final 块被认为是清理块,通常不会在其中返回。

【讨论】:

  • 我明白了,我应该在捕获结束时返回...谢谢
  • 但是有什么害处吗?如果我有意识地这样做,了解后果吗?
  • 我认为 OP 想在出现问题时返回 -1,在原始帖子中 -1 总是返回...
【解决方案2】:

finally 中的 return“覆盖”了进一步的异常抛出。

public class App {
    public static void main(String[] args) {
        System.err.println(f());
    }
    public static int f() {
        try {
            throw new RuntimeException();
        } finally {
            return 1;
        }
    }
}

1

【讨论】:

    【解决方案3】:

    通常,finally 块永远不应该有 return 语句,因为它会覆盖其他 return-statements 或 Exceptions。

    如需进一步阅读和更详细的背景答案,请查看问题

    Behaviour of return statement in catch and finally

    【讨论】:

      【解决方案4】:

      使用finally 块中的return 和throw 语句,您将收到警告,例如,您将收到与以下 finally 块相同的警告:

      ...
      }finally{
              throw new RuntimeException("from finally!");
      }
      ...
      

      【讨论】:

        【解决方案5】:

        如果您没有任何catch 块,那么您的finally 块必须直接嵌套在彼此内部。它只捕获一个异常,允许您的代码继续超过 try/catch/finally 块的末尾。如果没有捕捉到异常,finally 块之后就不能有任何代码!

        你可以看到它是如何工作的with this example on Repl.it

        示例输出

        testing if 0 > 5 ?
        try1
        try2
        finally3
        catch1
        finally2
        After other finally
        finally1
        end of function
        
        testing if 10 > 5 ?
        try1
        try2
        try3
        success
        finally3
        finally2
        finally1
           
        

        Repl.it 中的示例代码

        class Main {
        
            public static void main(String[] args) {
                isGreaterThan5(0);
                isGreaterThan5(10);
            }
        
            public static boolean isGreaterThan5(int a)
            {
                System.out.println();
                System.out.println("testing if " + a + " > 5 ?");
                try
                {
                    System.out.println("try1");
                    try
                    {
                        System.out.println("try2");
                        try
                        {
                            if (a <= 5)
                            {
                                throw new RuntimeException("Problems!");
                            }
        
                            System.out.println("try3");
        
                            System.out.println("success");
                            return true;
                        }
                        finally
                        {
                            System.out.println("finally3");
                        }
                    }
                    catch (Exception e)
                    {
                        System.out.println("catch1");
                    }
                    finally
                    {
                        System.out.println("finally2");
                    }
                    System.out.println("After other finally");
                }
                catch (Exception e)
                {
                    System.out.println("failed");
                    return false;
                }
                finally
                {
                    System.out.println("finally1");
                }
        
                System.out.println("end of function");
                return false;
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 2015-01-17
          • 2014-05-11
          • 2011-02-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-18
          • 2021-08-19
          • 2017-04-16
          相关资源
          最近更新 更多