Wednesday, June 18, 2014

Way to Catch Exception ( Custom Exception)

OVERALL EXCEPTION 

Data Access Layer : Catch and Log 

catch (Exception ex)
{
  CommonLogger logger = new CommonLogger();
  logger.LogError(ExceptionType.DACException, ex);

   throw new DBException(SQL_STATEMENT, country.CountryID.ToString(), ex);
   //or
   throw new DBException(SQL_STATEMENT, "", ex);
}

Business Layer : No log and Catch (you can throw Exception, but better throw custom exception or application exception)

Service Layer : Catch, log and form fault exception(if want to catch business layer exception, you want to catch another 1)

//Normal Exception
catch (Exception ex)
{
  string message = "Error retrieving Country. " +
((ex.GetType() == typeof(DBException)) ? "Unable to retrieve data from database.": "An exception occurred.");

   CommonLogger logger = new CommonLogger();
   logger.LogError(ExceptionType.Country, ex);

   PYFault fault = new PYFault(message);
   throw new FaultException<PYFault>(fault, message);
}

//Custom Exception
catch (PYException vex)
{
  PYResultFault fault = new PYResultFault(vex.Message, vex.PYResults);
  throw new FaultException<PYResultFault>(fault);
}

HOW TO CREATE FAULT EXCEPTION

[DataContract]
public class PYResultFault
{
      [DataMember]
      public List<PYResult> PYResults { get; set; }

      [DataMember]
      public string Message { get; set; }

       public PYResultFault(string message, List<PYResult> pyResults)
       {
            this.Message = message;
            this. PYResults = pyResults;
       }
}

Controller Layer : Catch (you can throw Exception, but better throw custom exception or application exception)**Can use back the Custom Exception

catch (FaultException<PYFault> fex)   
{
  throw new PYException(fex.Detail.Message, fex);
}
catch (FaultException<PYResultFault> fex)
{
   throw new PYException(fex.Detail.Message, fex.Detail.PYResults); 
}
catch (Exception ex) //Catch Connection Error
{
  throw new Exception(ex.Message);
}

Presentation Layer : Catch and Log Presentation Error

catch (PYException validateEx)
{
  StringBuilder sbrError = new StringBuilder();
  sbrError.Append(validateEx.Message);
  
  if (validateEx.PYResults != null)
  {
     sbrError.Append(": <br />");

     foreach (var result in validateEx. PYResults)
     {
       sbrError.Append(result.Message + "<br />");
     }
  }
    ModelState.AddModelError("Error", sbrError.ToString());
}
catch (Exception ex)
{
     CommonLogger logger = new CommonLogger();
     logger.LogError(ExceptionType.CountryManagement, ex);
    
     ModelState.AddModelError("Error",ConfigurationManager.AppSettings.Get("generalErr"));
}

WHEN NEED USE THIS METHOD TO CATCH EXCEPTION
when the an exception form at Business Layer with a list of information need to show.

This method is included custom and fault exception.
Custom Exception used when form a list of exception.
Fault exception used when to Transfer whole exception from WCF.

No comments:

Post a Comment