4

I want to catch SQLException on the try-catch block of Foo method and here is my code which is not working actually;

public int Foo() {
    try {
        DB.delete("table", "fname=" + name);
    } catch (SQLException e) {
        LOGGER.log(Level.WARNING, e.getMessage());
    }
}

public int delete(String table, String conditions) {
    int updatedRow = 0;
    try {
        String sql = "DELETE FROM " + table + " SET " + " WHERE " + conditions;
        updatedRow = SPM_Database.opeStmt.executeUpdate(sql);
    } catch (SQLException ex) {
        System.out.println("message" + ex);
        LOGGER.log(Level.WARNING, ex.getMessage());
    }
    return updatedRow;
}

I've got error from catch-block inside Foo() method in my IDE which is;

Unreachable catch block for SQLException

This exception is never thrown from the try-block. Why I cannot use the try-catch block? Is that I need to throw SQLException from delete() function or any ideas?

Levent Divilioglu
  • 11,198
  • 5
  • 59
  • 106
BufBills
  • 8,005
  • 12
  • 48
  • 90

3 Answers3

5

Your delete method can never throw a SQLException, since it doesn't declare it in a throws clause. Therefore, your catch clause in Foo is unreachable.

You don't need to throw SQLException from the delete method, but you also don't need to surround the call to delete with a try block and you don't need to catch SQLException.

StackFlowed
  • 6,664
  • 1
  • 29
  • 45
Eran
  • 387,369
  • 54
  • 702
  • 768
  • is it good practice to catch SQLException in lower level or higher level? I mean in delete() or the place I call delete()? – BufBills Mar 09 '15 at 20:35
  • 1
    @BufBills It depends on how you are going to handle it. If you just log the exception, it doesn't really matter where you catch it. If, on the other hand, you are going to try to recover, for example by retrying the delete operation, you should decide where is the best place to have the logic that performs the retry. It would probably make more sense to have this logic in a higher level. – Eran Mar 09 '15 at 20:39
2

The methode delete needs to throw the exception so Foo can catch it.

public int delete(String table, String conditions)  throws  SQLException{
        int updatedRow = 0;

                String sql = "DELETE FROM " + table + " SET " + " WHERE " + conditions;
                updatedRow = SPM_Database.opeStmt.executeUpdate(sql);


        return updatedRow;

Foo remains as it is.

good luck!

elsadek
  • 1,028
  • 12
  • 39
  • is it good practice to catch SQLException in lower level or higher level? I mean in delete() or the place I call delete()? – BufBills Mar 09 '15 at 20:35
  • 1
    In your case you should place catch in **Foo**, but in general it depends on the level of encapsulation that is applied. – elsadek Mar 09 '15 at 20:43
0

Add a throw statement in your catch block after what you need to do is complete. And also you need to add throws SQLException on your method signature.

public int delete(String table, String conditions) throws SQLException { // signature is changed
    int updatedRow = 0;
    try {
        String sql = "DELETE FROM " + table + " SET " + " WHERE " + conditions;
        updatedRow = SPM_Database.opeStmt.executeUpdate(sql);
    } catch (SQLException ex) {
        System.out.println("message"+ ex);
        LOGGER.log(Level.WARNING, ex.getMessage());
        throw ex;                                  //     <== ADD THIS LINE
    }

    return updatedRow;
}
Levent Divilioglu
  • 11,198
  • 5
  • 59
  • 106