I have to admit, I was under-whelmed in my first pass over the documentation for the new Enterprise Library. After all the hype, I expected to see more dramatic differences in functionality. My first impression was that the changes were mostly in refactoring the existing blocks. However, as I started to take a deeper look, features started to pop out at me. For example, the new Exception Handling Application Block.
The new Exception Handling Application Block goes way beyond the old Exception Management Application Block. There are three main concepts to understand in this block:
- Exception Handling
- Exception Logging
- Exception Policies
Exception Handling is the process of doing something with an exception when it is detected/caught by your code. There are three things you can have EHAP do to handle an exception. You can wrap the exception in a new exception to add new context information or detail. The original exception is still available as the InnerException when the new exception is propagated up the call stack. Second, you can replace the exception with a new exception. Do this when you don't want the details of the original exception to be propagated across an application boundary. The third thing you can do is log the exception. Of course, you can do this in combination with wrapping or replacing the exception, or you can log the original exception and propagate the original up the call stack.
Exception Logging makes use of another block, called the Logging and Instrumentation Application Block, to send formatted exceptions to the event log, an email message, WMI and several other targets. This alone gives us more options than what we had with the old block.
Finally, the component that really makes this particular block “hot” (yes, unfortunately, I did watch The Simple Life this week) is the Exception Policies. This allows you to define the way exception handling and logging works using external configuration files instead of baking the rules into your code. In other words, you can define the exception handling in a policy file and then change the behavior for different testing, debugging and production scenarios without changing your code. Your administrators can manage the policy file themselves to make your application more secure by hiding sensitive information from users during an exception. Or they can define policy to produce additional information for consumption by management applications such as Microsoft Operations Manager.
The basic pattern to integrate your code with this block is documented as the following:
// run code
bool rethrow = ExceptionPolicy.HandleException(ex, "General Policy");
Pretty simple. The Exception Handling Application Block in Enterprise Library is definitely something to dig further into as it provides a lot of new functionality that did not exist in the old Exception Management Application Block. It also provides the flexibility to create a robust application that you can still monitor and debug without exposing too much detail to the outside world.