Wrapping an Exception
A frequently required exception-handling task is wrapping one exception with a different exception. Wrapping an exception creates a new exception of a defined type and sets the original exception as the InnerException object of the new exception. Use the wrapping capability in situations where the original exception type must be mapped to a new exception type for use by other tiers within the architecture of the application. You can encapsulate and interpret details of the underlying layer's original exception without losing any details about that exception. You can wrap the original exception either in an existing exception type or in a custom exception type that you create. The following explains a situation when you would want to wrap an exception:
- A business service named Update Customer calls a data layer service.
- The data layer service fails and throws an exception. This could be any one of many exceptions that indicate that the update failed. Some sets of these exceptions indicate that recovery may be possible with a retry (for example, if a record is locked) while others are non-recoverable (for example, if there is a concurrency violation or a dirty record).
- The exception handler maps and wraps these sets of exceptions into two custom exception types, RecoverableUpdateException and FatalUpdateException.
- The business service handles the exception based on the wrapping type and takes the appropriate action, even though it is insulated from detailed knowledge of the underlying failure.
Typical Goals
You want to wrap one exception within another. Your application has code in its catch blocks similar to the following.
catch(SomeException e)
{
CustomException myException = new CustomException(e);
throw myException;
}
'Usage
Catch e As SomeException
Dim myException As CustomException = New CustomException(e)
Throw myException
End Try
Solution
Use the wrap handler in your exception handling chain.
Using the Wrap Handler
The following procedure describes how to use the wrap handler.
To use the wrap handler
Create an exception handling policy that includes the appropriate exception types for your application. For more information, see Entering Configuration Information.
Configure the exception type, specifying the post-handling action as ThrowNewException. This means that the block throws the new exception that has been created by wrapping the original exception. The throw occurs after the entire chain of handlers runs.
Add a new wrap handler for the specified exception types.
Configure the wrap handler. Add the exception message to be used and the exception type that will wrap the original exception.
Modify your application code to execute the new policy when an exception occurs, as shown in the following code. It assumes you have resolved an instance of the ExceptionManager class through the Enterprise Library container and saved it in a variable named exManager. Substitute the name of your own policy for "Wrap Policy." For more information on instantiating objects, see Creating and Referencing Enterprise Library Objects.
exManager.Process(YourTargetMethod, "Wrap Policy");
'Usage exManager.Process(AddressOf YourTargetMethod, "Wrap Policy")
Usage Notes
Consider the following information when you configure the application block to use a wrap handler:
- Typically, exceptions should be wrapped after they are logged in their original state.
- To help in the management and tracking of exceptions, the block generates a HandlingInstanceId object that you can use to map to the original exception. To use the HandlingInstanceId, you must include the {handlingInstanceId} token in the exception message that is in the configuration file. The HandlingInstanceId is of type GUID. For more information, see Assisting Support Staff.
- Because the post-handling action is set to ThrowNewException, the exception thrown by the application block is the final exception that results from running the entire chain of handlers. For example, if the chain specifies that exception A is to be wrapped with exception B, and a later handler replaces exception B with exception C, the block throws exception C when the post-handling action is set to ThrowNewException.
- The exception type used to wrap another exception must have a constructor that accepts two parameters: a string and an exception object. This means that you must provide this constructor overload for all custom exception types that are used to wrap other exceptions.