"finally block did not complete normally" Eclipse warning

Eclipse will tell me this warning in the following code:

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

What is wrong with my code?

+67
java try-catch-finally
Jul 05 '13 at 4:47
source share
5 answers

remove the returned statement from it. The final block is considered to be a cleaning block; usually a return is not expected in it.

+120
Jul 05 '13 at 4:50
source share

return from finally "overrides" a further throw exception.

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

one

+22
08 Oct '14 at
source share

Typically, a finally block should never have a return statement, because it overwrites other return stats or Exceptions .

For further reading and more detailed answers to his source materials see question

The behavior of the return statement in catch and finally

+8
Dec 11 '14 at 9:39
source share

With the return and throw instructions in the finally block, you get a warning, for example, you get the same warning with the following finally block:

 ... }finally{ throw new RuntimeException("from finally!"); } ... 
+4
May 13 '16 at 9:20
source share

If you do not have catch blocks, then your finally blocks should be nested. It only catches the exception that allows your code to continue after the end of the try / catch / finally block. If you do not understand the exception, you will not have code after the finally block!

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

Output example

 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 the example on 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; } } 
0
Nov 07 '18 at 16:59
source share



All Articles