I have an application with DAL and Service Layer(WCF Rest). I had errors in Service Layer by using IErrorHandler.

In DAL layer i use try catch in all method and log into the error into a text file in catch. After that i used throw to throw the error.

My DAL layer method like below

public DataTable Method1()
{
    DataSet dsData = null;
    try
    {
            //code to get the data
    }
    catch (Exception ex)
    {
        dbManager.WriteErrorLog(module, MethodBase.GetCurrentMethod().Name, ex.Message);
        throw;
    }
    return dsData.Tables[0];
}

Here in catch i used a general method called WriteErrorLog to write the exception to the text file.

My confusion is we already handled error in Service Layer. So can i avoid using try .. catch in my DAL. or the existing is good practice for DAL?

有帮助吗?

解决方案

Well, you're not really handling the exception since you are re-throwing and bubbling it up to the service layer which is exactly what you want to do if you want to log exceptions occurring in your data access layer.

So, you are doing exactly what needs to be done and it is indeed a good practice to:

  1. Log errors
  2. Not to handle exceptions in the lower layers of an application system
  3. Let the front-end/consuming applications handle exceptions and/or errors
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top