development

'최종 차단이 정상적으로 완료되지 않음'Eclipse 경고

big-blog 2020. 11. 10. 21:50
반응형

'최종 차단이 정상적으로 완료되지 않음'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;
    }
}

내 코드에 어떤 문제가 있습니까?


그것에서 return 문을 제거하십시오. 최종 블록은 정리 블록으로 간주되며 일반적으로 반환이 예상되지 않습니다.


return에서 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


일반적으로 finally블록은 다른 return문 또는을 덮어 쓸 수 있으므로 return 문이 없어야 Exceptions합니다.

자세한 내용과 배경에 대한 자세한 답변은 질문을 참조하십시오.

catch 및 finally에서 return 문 동작


블록에 returnthrow이 모두 finally있으면 경고가 표시됩니다. 예를 들어 다음 finally 블록과 동일한 경고가 표시됩니다.

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

If you don't have any catch blocks, then your finally blocks have to be nested directly inside of each other. It's only catching an exception that allows your code to continue past the end of a try/catch/finally block. If you don't catch the exception, you can't have any code after a finally block!

You can see how this works with this example on Repl.it

Example Output

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

Code in example at 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;
    }

}

참고URL : https://stackoverflow.com/questions/17481251/finally-block-does-not-complete-normally-eclipse-warning

반응형