Enterprise Library 5.0 Exception Handling includes the Wrap Handler. This wraps an exception in another exception and throws the wrapper exception.
I would like to add additional information to my wrapper exception, such as some information about the parameters and settings that were in effect when the error occurred. This would be handy for troubleshooting (since this wrapper exception will be caught and logged by a higher-level handler).
I was using the following code:
ExceptionManager em = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
em.Process(() => { DoSomething(); }, "WrapPolicy");
The result of this is an exception that gets wrapped in my custom exception and thrown. This is clean and easy, but since my code never sees the wrapper exception instance, I can’t augment it with additional information.
Other than wrapping the action in a traditional Try…Catch and executing the HandleException method manually, is there a way to inject additional information into the wrapper exception instance?
I’m not sure what kind of additional information you want to add but if you want to maintain the elegance of the Process method then you could create a custom WrapHandler that adds the appropriate information to the wrapper exception. A good candidate would be to add the additional information to the Exception.Data IDictionary.
If you are looking for method arguments then probably the only way to do it would be by creating your own “context”, setting values in there, and extracting them from the context to add to the wrapping exception.
Another more elegant approach would be to use Policy Injection. Call handlers have access to the method parameters as well as the exception thrown. You could modify the existing ExceptionCallHandler to add your information to the original or new exception. If you add the context information to the original exception then that information would be processed by the Exception Policy (which could include logging).