In a Java EE legacy project, almost all the DAO and Service classes are written in a a way that DAO level does not catch any exception and instead the service classes catch(Exeption e)
in all of their methods. The following is an example when adding a new method to the DAO and Service
A new find method, findByStatus
, is a legacy DAO class AccountDAO
public List<Account> findByStatus(Status status) {
return entityManager.createQuery(
"FROM Account a WHERE a.status = :status"
)
.setParameter("status", status)
.getResultList();
}
And then it is used in a service class
public List<Account> findByStatus(Status status) {
List<Account> retVal = new ArrayList<>();
try {
retVal = accountDAO.findByStatus(status);
} catch (Exception e) {
LOG.error("Error in findByStatus", e);
}
return retVal;
}
The code above can work since the code has been in production for more than ten years, but
QUESTION: Is this a matter of preference to handle errors on the DAO or service level? And should the new method be added in the same way, i.e. letting service classes catch(Exception e)
instead of ignoring any exception in the DAO class level, for the sake of consistency with legacy code?