Abstracting EHAB as separate layer in the solution?

Topics: Exception Handling Application Block
Oct 15, 2007 at 2:46 AM
I am trying to configure Exception Handling Application Block for my solution which has multiple projects. I want to show a custom message box whenever an exception is caught in any project in the solution. I abstracted EHAB components into a separate class library project so that I can reference it in various projects in my solution. This is what I did:

1. Created a new Class Library project called "GeneralExceptionHandling".
2. Added references to Microsoft.Practices.EnterpriseLibrary.Common.dll, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll and Microsoft.Practices.ObjectBuilder.dll
3. Created custom exception handler class called "AppMessageExceptionHandler" that displays a dialog to notify user of an exception.
4. Added a static class called "ExceptionPolicy" with a static method called "HandleException" that can be called from various projects when a exception is caught. This is how the static class looks like

public static class ExceptionPolicy
public static bool HandleException(Exception exceptionToHandle)
return HandleException(exceptionToHandle, "Global Policy");

public static bool HandleException(Exception exceptionToHandle, string policyName)
return Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(exceptionToHandle, policyName);

5. Added a App.Config class
6. Using Entripise Library configuration tool added a exception handling policy called Global Policy
7. Added System.Exception to the policy.
8. Changed the PosthandlingAction as None.
9. Addded a new custom handler, settign the handler type to handler AppMessageExceptionHandler I created before. I did this by clicking Load an Assembly button on Type Selector dialog and selecting GeneralExceptionHandling.dll in the bin\Debug folder of the project.

Now when I try to handle exception in a project using following code in get “Object reference not set to an instance of an object.” Exception

void MethodA()
throw new Exception();
catch (Exception ex)
bool rethrow = GeneralExceptionHandling.ExceptionPolicy.HandleException(ex);
if (rethrow)


On stepping through the code it fails at the stage where it tries to create the policy by calling factory.Create(policyName) method in Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.GetExceptionPolicy() method

What is going wrong here? I am confused. Please help.


Oct 18, 2007 at 4:43 AM

have you found a solution to this issue yet? If you have I'd be glad to know. I have the same issue but I can make it work if I add the policy to the config file for the main calling application and not the handling DLL.

Thanks Martin