Wrapping an exception using Wrap handler

The Exception Handling block provides an out-of-the-box handler called Wrap Handler, which allows us to configure the wrap exception type and the exception message. We can also load the exception message from a resource file by specifying the message resource name and resource type. Based on the configuration, the exception is wrapped using the new exception type with the specified exception message. The new exception object contains the original exception as part of the InnerException.

Wrapping the original exception with a new exception type is useful in the scenarios given next:

  • Updating the error message of the original exception with a more meaningful message while maintaining the original context
  • Throwing a specific exception (DataLayerException, BusinessLayerException, FatalException, NonFatalException, and so on) across layers/boundaries while maintaining the original context

Configuring a Wrap exception handler

We currently have the default settings in the configuration file; to understand the configuration we will delete the default policy named Policy. The steps to configure Wrap Handler are given as follows:

  1. Add a new policy in the policies section and name it Data Access Policy as shown in the following screenshot.
    Configuring a Wrap exception handler
  2. Right-click on the Data Access Policy and click on Add Exception Type. This will pop up a new exception type selection dialog.
    Configuring a Wrap exception handler
  3. Specify the type name as DbException by keying in the type name.
    Configuring a Wrap exception handler
  4. Set the Post handling action attribute of the exception type DbException to ThrowNewException.
    Configuring a Wrap exception handler
  5. So far we have added the policy and the exception type, now let's add the Wrap Handler. Right-click on the exception type DbException and click on Add Handlers | Add Wrap Handler.
    Configuring a Wrap exception handler
  6. Set the exception message and also set the Wrap Exception Type to System.ApplicationException.
    Configuring a Wrap exception handler

So far we have successfully configured the application with a policy that will process an exception of type DbException or any matching exception in the class hierarchy. It also wraps the exception with a new System.ApplicationException with the specified exception message. Once the exception is wrapped with the new exception object, the new exception is thrown as configured in the post-handling action.

The following is the execution result of Exception Wrapping Demo provided as part of the sample application with this book:

Exception Message:
===================
Wrapped Exception: Database operation failed due to concurrency issue. Error code: f294419a-b4b5-47ad-9e9e-ec62362965f2
Inner Exception Message:
=========================
Original Exception: Concurrency violation: the UpdateCommand affected 0 records.

We can see in the given result that the original exception is wrapped with a new exception message. This helps in retaining the context yet providing more meaningful information to the application user.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.26.22