我有一个三层结构:
1. 展示层
2. 业务层
3. 数据层
展示层通过服务门面与业务层交互。现在我困惑于应该在哪里抛出异常、在哪里记录异常以及在哪里捕获并忽略异常。目前,在记录日志后,我在我的展示层中忽略异常,而在其他地方,我在记录日志和抛出异常。这导致异常被记录了三次。虽然这不是一个巨大的问题,但我想知道最佳实践是什么。
例如: 展示层代码:
try
{
UserService.GetAllUsers() ;
}
catch(Exception ex )
{
Logger.log(ex) // exception gets logged here
// redirect to a friendly user error page
}
用户服务层代码
try
{
IUserDAO userDAO = ServiceRegistry.GetRegistry().GetDAOFactory().GetUserDAO() ;
return userDao.GetAllUsers() ;
}
catch ( Exception ex)
{
Logger.log(ex) ; // and here !
throw;
}
DAO层代码
try
{
// All db interaction code
}
catch(Exception ex)
{
Logger.log(ex) //and here !!
throw ;
}