79

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

我的代码有什么问题?

4

5 回答 5

137

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

于 2013-07-05T04:50:24.427 回答
31

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

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

于 2014-10-08T21:35:49.197 回答
13

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

有关进一步阅读和更详细的背景答案,请参阅问题

catch 和 finally 中 return 语句的行为

于 2014-12-11T09:39:01.843 回答
5

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

...
}finally{
        throw new RuntimeException("from finally!");
}
...
于 2016-05-13T09:20:01.487 回答
0

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

你可以在 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;
    }

}
于 2018-11-07T16:59:34.677 回答