有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java“最终块未正常完成”Eclipse警告

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

我的代码怎么了


共 (5) 个答案

  1. # 1 楼答案

    来自finallyreturn“重写”进一步的异常抛出

    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

  2. # 2 楼答案

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

    ...
    }finally{
            throw new RuntimeException("from finally!");
    }
    ...
    
  3. # 4 楼答案

    如果没有任何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。它

    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;
        }
    
    }
    
  4. # 5 楼答案

    从中删除return语句。 最后一个区块被视为清理区块,通常不会在其中返回