The question is not about exception handling syntax, but it is all about which is the right place to write catch block for an exception in its journey over methods through propagation.
public boolean validateUser(String username, String password) throws SQLException {
Connection conn = dataSource.getConnection();
boolean result = false;
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM USERS WHERE USERNAME=? AND PASSWORD=?");
pstmt.setString(1, username);
pstmt.setString(2, password);
result = pstmt.executeQuery().next();
conn.close();
return result;
}
Assume method1()
called method2()
and method2()
called above method. In the above method if I handle the exception, I have to return a value. Let's assume that I have returned false
after catch block, method2()
misunderstands like the username or password is wrong.
If I am not handling, method2()
will not receive any value and it's catch block will execute and the same problem would occur in method1()
.
Now can you define where can I effectively handle the exception?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…