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 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.
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 application 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.
- If 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 application block throws exception C when the post handling action is set to ThrowNewException.
- If the post handling action is set to NotifyRethrow, the application block returns true to the calling code. This allows the code to throw the original exception.
- Application code should always check the return value instead of assuming that it is the same value as the configured value. Even if the configuration data changes, if the application code checks the return value, the application code will function correctly and will not need to be modified.
- 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.
- If you use the Unity Integration approach to create instances of objects from the Exception Handling Application Block, you must use the non-static façade named ExceptionManager instead of the ExceptionPolicy class static façade.