Handling Dao exceptions in service layer

后端 未结 4 1488
温柔的废话
温柔的废话 2020-12-13 04:47

If my Dao layer throws Dao specific exceptions, then does handling them in my service layer consitute a leakage of concerns? If yes, then should I make the exceptions generi

4条回答
  •  借酒劲吻你
    2020-12-13 05:04

    Your DAO layer already leaks into the service layer when you do operations like:

    userDAO.persist(user);
    

    Exceptions, being a part of the API just like operation should be considered in the same way.

    try {
        userDAO.persist(user);
    } catch (DAOException e) {
        // looks fine to me
    }
    

    Leakage can happen with runtime exceptions, or when you rethrow exceptions

    try {
        userDAO.persist(user);
    } catch (SQLException e) {
        // sql implementation exposed
    }
    

    But even this sounds better than "layer independent" exceptions

提交回复
热议问题