Using the ExceptionPolicy Class
Whenever you work with the exception handling block, you will have to deal the ExceptionPolicy class, which exposes a static method named HandleException()
that lets the client application interact with the Exception Handling Block. You supply the policy as an argument. The HandleException
method uses a factory to create an object of type ExceptionPolicyImpl for the supplied policy. The ExceptionPolicyImpl object has a collection of ExceptionPolicyEntry objectsone object for each exception type specified in the configuration file for the named policy. For each exception type, the ExceptionPolicyEntry object contains a collection of objects that implement the IExceptionHandler interface. The collection is ordered and provides the sequence that the exception handling block uses when executing the policy. Each object that implements the IExceptionHandler interface has associated configuration information that is specific to each type of handler.
Exception handlers are .NET classes that encapsulate exception handling logic and implement the IExceptionHandler interface defined in the Exception Handling Block. By default, the Exception Handling Block includes three exception handlers:
. This exception handler wraps one exception around another.
. This exception handler replaces one exception with another.
. This exception handler the formatting of exception information, such as the message and stack trace. The logging handler gives this information to the logging block for publication.
You can extend the Exception Handling Block if required by implementing your own handlers using the Enterprise Library Configuration Tool. The nice thing about this approach is that you don't have to modify and rebuild the application block to extend it.
Handling Exceptions Using ExceptionPolicy
To demonstrate using the Exception Handling Block, here's a simple example. You'll create a simple Windows forms application named ExceptionMgmtBlockExample
. Create the project in Visual Studio, add the references listed earlier, and then open the default form in the designer and add a Command button named btnHandleException
. Modify its Click
event to look as follows:
private void btnHandleException_Click
(object sender, EventArgs e)
throw new Exception
("This is a test exception");
catch (Exception ex)
bool rethrow =
(ex, "Global Policy");
In the try
block, you simply throw an exception, which the catch
block will catch, invoking the HandleException()
method of the ExceptionPolicy class, passing the "Global Policy" as the policy name argument. As shown in the previous section, the "Global Policy" is associated with the exception handler named AppMessageExceptionHandler, whose declaration is as follows:
using Microsoft.Practices. EnterpriseLibrary.Common.
public class AppMessageExceptionHandler :
public Exception HandleException
(Exception exception, Guid correlationID)
DialogResult result = this.
if (result == DialogResult.Abort)
private DialogResult ShowThreadExceptionDialog
string errorMsg = e.Message +
"Application Error", MessageBoxButtons.OK,
|Figure 2. Sample Exception: Here's the exception you get from running the sample Windows application.|
As you can see, the custom handler derives from the IExceptionHandler interface. The HandleException
method invokes another helper method named ShowThreadExceptionDialog
, which formats the exception message and displays it on the screen.
If you launch the Windows application and click on the "Handle Exception" button, you will see the message box shown in Figure 2