Dev
Dev

Reputation: 317

Error Message during Exception .

Hi i have following method on my DataAccess Layer : Its fine but on Try Catch if there is error or SQL Exception i am logging it but it will return null to business logic layer in case of exception . But i want to handle it if there is error it should inform Business logic layer about error. Any idea how could i do that.

public DataTable  PapoulateIndexAnalyssList()
{
    DataTable dt;
    dt = new DataTable();
    try
    {
        this.indiceTableAdapter.FillBy(this.ds_IndexAnalysis.Indice);
        dt = this.tmp_Table_Analisi_IndexTableAdapter.GetData();
    }
    catch(Exception ex)
    {
        //Logging Error:
        errorMessageVieReportDAL = ex.Message;
        Logs.WriteLog(errorMessageVieReportDAL);
        ObjResults.SetFailure = false;
        ObjResults.GetErrorMessage = errorMessageVieReportDAL;
    }
    return dt;
}

Upvotes: 1

Views: 99

Answers (3)

Kristof
Kristof

Reputation: 3315

If you insist on not rethrowing an exception you can always wrap your result in a response object.
The calling code would have to check if the result is in error state or not.
Your code would look something like this :

public QueryResult{
  public DataTable Data{get;set;}
  public bool Error{get;set;}
  public string ErrorMessage{get;set;}
}

public QueryResult PapoulateIndexAnalyssList()
{
    var result = new QueryResult();
    result.Data = new DataTable();
    try
    {
        this.indiceTableAdapter.FillBy(this.ds_IndexAnalysis.Indice);
        result.Data = this.tmp_Table_Analisi_IndexTableAdapter.GetData();
    }
    catch(Exception ex)
    {
        //Logging Error:
        errorMessageVieReportDAL = ex.Message;
        Logs.WriteLog(errorMessageVieReportDAL);
        ObjResults.SetFailure = false;
        ObjResults.GetErrorMessage = errorMessageVieReportDAL;
        result.Error = true;
        result.ErrorMessage = ex.Message;
    }
    return result;
}

Upvotes: 0

ekad
ekad

Reputation: 14614

You shouldn't return null or new DataTable() if an error happens inside the data access layer, it will be ambiguous between no records found or an error happens. You need to rethrow the exception from the data access layer

catch(Exception ex)
{
    //Logging Error:
    errorMessageVieReportDAL = ex.Message;
    Logs.WriteLog(errorMessageVieReportDAL);
    ObjResults.SetFailure = false;
    ObjResults.GetErrorMessage = errorMessageVieReportDAL;

    throw;
}

and catch the same exception in the presentation layer, then show some user friendly error message so user will be aware that an error happens.

Code in presentation layer:

DataTable dt = null;

try
{
    dt = BLL.PapoulateIndexAnalyssList();
}
catch
{
    // code to show user friendly error message here
}

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66439

You're instantiating the DataTable before the try/catch, so it won't be null, it'll be empty.

If you used this instead, then it would be null, if you feel that's enough information for the calling method to be able to determine that PapoulateIndexAnalyssList failed.

DataTable dt = null;

It's acceptable to act on the error (such as log it), but then rethrow it so that methods up the chain know about it too, and that's probably the correct way to handle this.

catch (Exception ex)
{
    //Logging Error:
    errorMessageVieReportDAL = ex.Message;
    Logs.WriteLog(errorMessageVieReportDAL);
    ObjResults.SetFailure = false;
    ObjResults.GetErrorMessage = errorMessageVieReportDAL;

    throw ex;
}

If the calling method needs to have an empty table, then that's the concern of the calling method, not PapoulateIndexAnalyssList. The calling method should handle that in its own catch block (in my opinion).

DataTable dt;

try
{
    dt = someClass.PapoulateIndexAnalyssList();
}
catch
{
    dt = new DataTable();
}

Upvotes: 1

Related Questions