Tuesday, January 11, 2011

Enterprise Library 5 Exception Handling

Last week I discovered a nice feature inside the Enterprise Library 5 Exception Handling application block. On the ExceptionManager class a new method is available called Process(). This method automatically performs exception management and throws the exception based on the configuration. It accepts the policy name and a delegate or a lambda expression; the application block manages any exception that occurs while executing the method or lambda expression, also if the postHandlingAction is set to ThrowNewException then the application block throws the exception as a result of the respective execution of the configured exception handlers.

This minimizes the amount of exception logic you need to write a lot. Typical usage of the Process method will be similar to the code snippet given next:

   1:  //Get instance of ExceptionManager using static method of Enterprise Library Container
   2:   
   3:  ExceptionManager exManager = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
   4:   
   5:  Order order = null;
   6:  OrderRepository orderRepository = new OrderRepository();
   7:   
   8:  //try..catch block not required...
   9:  //Automatic Exception Management through Process method
  10:  order= exManager.Process<Order>(
  11:     () => 
  12:        { 
  13:           return orderRepository.GetOrder(1000); 
  14:        }
  15:     ,"Data Access Policy");

1 comment:

Anonymous said...

That's handy but how do you accomplish a finally block as you would with a traditional try/catch/finally code block?

There is no try/catch when using Process method.