【发布时间】:2013-01-15 17:43:09
【问题描述】:
我在我的应用程序中遵循了这种处理异常的方式。但是我的领导说我做错了。我只是包装并重新抛出相同的异常,这会影响性能。
我的方法有什么问题?有人对我如何在这里记录和处理异常有任何建议吗?
public class BusinessRepository : IBusinessRepo
{
public List<Employee> GetEmployees()
{
try
{
//do some DB operations
}
catch (SQLException sqlex)
{
Logger.Log("Exception detail with full stack trace");
throw new DALException(sqlex, "Error in data access layer");
}
}
}
public class BusinessLayerClass : IBusinessLayer
{
private readonly IBusinessRepo Repo;
public BusinessLayerClass(IBusinessRepo rep)
{
Repo = rep;
}
public List<Employee> GetEmployees()
{
try
{
List<Employee> emps= return Repo.GetEmployees();
}
catch (DALException dex)
{
//do nothin as it got already logged
throw;
}
catch (Exception ex)
{
Logger.Log(ex, "Business layer ex");
throw new BusinessLayerEx(ex);
}
}
}
public class HomeController : Controller
{
public ActionResult Index()
{
try
{
List < Employee >= BusinessLayerClass.GetEmployees();
}
catch (DALException)
{
//show error msg to user
}
catch (BusinessLayerEx)
{
//show error msg to user
}
catch (Exception ex)
{
Logger.Log();
//show error msg to user
}
return View(emps);
}
}
我是否遵循上述正确的冒泡、处理和记录方式?
【问题讨论】:
-
根据 john skeet 的说法,异常抛出几乎不会影响性能。见:developerfusion.com/article/5250/…
-
既然异常应该是异常的,那么重要吗?
-
@albertjan 我同意 Jon Skeet。但是我的方法是正确的处理和记录方式吗?
-
我发现真正令人震惊的是“它已经被记录”的断言,我不是指语法。这只能意味着在 Repo 中有 另一个 try-catch 引诱。
标签: c# asp.net-mvc asp.net-mvc-3 exception-handling