栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

三层架构和例外

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

三层架构和例外

那么,您的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);      }   }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/496792.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号