我按照這種方式處理我的應用程序中的異常。但是我的領導說我做錯了。我只是打包並重新拋出同樣的異常,這會影響性能。打包並重新拋出異常會影響性能嗎?
我的方法有什麼問題?有沒有人對我如何在這裏登錄和處理異常有任何建議?
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);
}
}
我是否遵循如上圖所示興高采烈和處理和日誌記錄的正確方法嗎?
根據約翰斯波特的表現幾乎不會受到異常投擲的影響。請參閱:http://www.developerfusion.com/article/5250/exceptions-and-performance-in-net/ – albertjan
由於異常應該是例外,它有多重要? – emartel
@albertjan我同意Jon Skeet。但是,我的方法是處理和登錄的正確方法嗎? – Billa