那么,您的Dao异常与服务层无关,并且服务层与dao层异常无关。正确的方法是捕获dao异常,然后将新的自定义异常扔到服务层。
如果需要调试异常并需要确切原因,则可以使用getCause()和getSuppressed()方法。
我应该用try-
catch块包装此DAO方法调用,然后将可能的异常作为ServiceException抛出吗?每个DAO方法都应该只抛出PersistenceException吗?
-–>是的,把它包起来。您可以从dao层抛出其他异常。请参阅以下示例:
public class MyDao { public Entity getMyEntity(int id) throws ObjectNotFoundException, PersistenceException { try { // pre to get Entity // if entity not found then throw new ObjectNotFoundException("Entity with id : " + id + "Not found."); } catch(Exception e) { // you can catch the generic exception like HibernateException for hibernate throw new PersistenceException("error message", e); } }}


