服务/dao/业务层中的java最佳实践异常处理
当错误被抛出到不同的层时,处理异常的最佳实践是什么
我有4层代码——DAO、服务、业务和演示。我试图捕获dao层中的一些运行时异常,并希望它在表示层中显示一些消息。下面的方法好吗
在代码片段中,DataException是我的运行时异常类。服务和业务异常类是我检查过的异常实现类
下面是代码片段:
在dao层,一个方法从数据库中检查一些值
class dao{
public User getUser() throws DataException{
User user = null;
try
{
//some operation to fetch data using hibernatetemplate
}catch(Exception ex){
throw new DataException(ex.getMessage());
}
return user;
}
}
服务。爪哇
class service{
public User getUser(String username) throws ServiceException{
User user = null;
try
{
//some operation to fetch data using dao method
dao.getuser(username);
}catch(DataException ex){
throw new ServiceException(ex.getMessage());
}
return user;
}
}
生意。爪哇
class business{
public User getUser(String username) throws BusinessException{
User user = null;
try
{
//some operation to fetch data using dao method
service.getuser(username);
}catch(ServiceException ex){
throw new BusinessException(ex.getMessage());
}
return user;
}
}
在表示层,让它成为一个控制器类
class Presentation{
public User getUser(String username) throws BusinessException{
User user = null;
//some operation to fetch data using business method
business.getUser(username);
return user;
}
}
假设在前端JSP页面中,从表示层向用户抛出消息
共 (0) 个答案