Exception Handling

Oct 19, 2007 at 12:38 PM
Hi Rajesh,

Can you tell Best way of Catching Exception in Layer Architecture?

Example:

DataAcces Layer
************************
public void SaveOrderForm(ref Deals objDeal)
{

some code;

}
catch (Exception ex)
{
bool rethrow =
ExceptionPolicy.HandleException(ex, "DataAccess Layer Policy");
if (rethrow)
{
Logger.Write("SaveOrderForm()", ex.ToString());
throw new Exception("Data is not saved through CustOrderFormDataAccess SaveOrderForm()");
}

}

I am Calling this method in Business Logic Layer

Business Logic Layer:
***************************
public void SaveOrderForm(ref CustOrderForm objOrderForm)
{
try
{
CustOrderFormDataAccess.GetInstance().SaveOrderForm(ref objOrderForm);

}
catch (Exception ex)
{
bool rethrow =
ExceptionPolicy.HandleException(ex, "Business Layer Policy");
if (rethrow)
{
Logger.Write("BusinessRules-->SaveOrderForm", ex.ToString());
throw new Exception("Data is not Inserted through OrderFormBusinessRules SaveOrderForm(CustOrderForm objOrderForm)");
}
}
}

i am calling this method in Business Facade Layer

Business Facade Layer
****************************

public void SaveOrderForm(ref CustOrderForm objOrderForm)
{
try
{
using (OrderFormBusinessRules objOrderFormBusinessRule = new OrderFormBusinessRules())
{
objOrderFormBusinessRule.SaveOrderForm(ref objOrderForm);
}
}
catch (Exception ex)
{
bool rethrow =
ExceptionPolicy.HandleException(ex, "Facade Layer Policy");
if (rethrow)
{
Logger.Write("OrderFormFacade-->SaveOrderForm", ex.ToString());

throw new Exception("Data is not Inserted through OrderFormFacade SaveOrderForm(CustOrderForm objOrderForm)");
}
}

Calling this method in Presentation Layer

Presentation Layer
***********************
Catching Exception in this layer.

This is Right way way of Catching Exception in Enterprise Architecture.

Plz Correct my code if going wrong direction...

Questions
*********************
Why we go for Custum Expection?


Is it required to go for Custom Exception Block Layer, when i using
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
Microsoft.Practices.EnterpriseLibrary.Logging;



Coordinator
Oct 22, 2007 at 6:42 AM
These are some rules that one could follow for exception handling in your applications
  1. Define your error space /Know your exceptions - For every layer, you should to define the exceptions that can occur in that layer as well as the exceptions that you want to throw to the layer above you.
  2. Define a consistent exception handling policy/strategy - For every exception that occurs, you can either replace, wrap, log or supress the exception. You need to define this strategy for each layer as well for each type of exception. The Exception Handling block that is part of Ent Lib allows you to do this with very little amount of code (Mostly via configuration).
  3. Only catch exceptions in case you want to handle them. If you are just rethrowing them, then you dont need to have the try/catch handlers.
  4. Each layer should have a custom exception class to which all exceptions in that layer are wrapped. Ensure that the original exception is sent as the inner exception property.
  5. Make use of global exception handlers whenever possible to reduce the amount of try/catch handling code. You can easily do that in an ASP.NET Web Application (using Application_OnError wire up) or in Window/Console Applications (UnhandledException wire up) or WCF Services (Faulted State wire up).

Some suggestions based upon your code...
  1. You have written the code to log exceptions in your catch block. This can be defined in the policy itself.
  2. You are always throwing a new exception (using the replace strategy). Try using the Wrap strategy, where you would like to have the inner exception details preserved for upper layers in case they wish to use them. For example, your UI layer wants to display the message of the exception.
In your case, irrespective of any error in the DB layer (for e.g. SQL Server not accessible, stored procedure not found etc.), the exception that will be thrown will always have the same custom message that you created which may not be very useful.

Custom Exceptions...
As mentioned in the rules above, it is always a good idea to have a custom exception class in each layer. This allows your clients (i.e. layers above you) to catch a single exception type for all errors that occur in the layers below. For that you need to wrap all exceptions in that layer to a single exception type. As an example, we have defined a DBLibException class in the DAL Layer, Business Exception in the BLL and a AppointmentFaultException for the services layer. We are using the Wrap strategy. Check out the App.config file in the Hosts project to check out our Exception Handling policies. Also, check out the Exceptions folder in the EAppointments.BMS project for specific business exceptions.

You can take a look at each layer in the E-Appointments application for the strategy used to get a better understanding. The UI Layer lacks the global exception handler in this release.

Do let me know if you have any further questions.

- Rajesh
Oct 22, 2007 at 8:45 AM
Thank you Very much Rajesh,

Now I got good Understand about Expection Handling in each layer.