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:
DataLayerException, BusinessLayerException, FatalException, NonFatalException
, and so on) across layers/boundaries while maintaining the original contextWe 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:
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.
3.129.26.22