“最终阻止无法正常完成” Eclipse警告


72

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
删除返回-1; 它应该没问题
萨蒂亚(Satya)

@Dennis“最终阻止无法正常完成”,他在主题行中写了这
句话

在try-catch-finally块之后添加return -1。
danpop

Answers:


129

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


3
我明白了,我应该在catch结束回...谢谢
何塞·D·

2
但是有没有害处?如果我有意识地做到这一点,了解其影响?
Dhiraj

我认为OP会在出现问题时返回-1,在原始帖子-1中始终会返回...
Bamboomy

27

returnfinally“覆盖”进一步异常抛出。

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个



5

在块中同时使用returnandthrow语句时,finally您将得到警告,例如,您将在以下finally块中得到相同的警告:

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

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

}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.