Exception Handling Application Block - How to replace the original message with mine based on some properties of the original message?


I'm creating a class library which uses aggregated (built-in) COM object. Methods and properties of this COM-object may throws COMException, which ErrorCode property is the HRESULT indicates errors in COM-object's internal logic.

For example, HRESULT (and, therefore, COMException.ErrorCode) = -1001 means that input parameter of a particular method has a bad format.

I want to utilize Exception Handling Application Block in my library and replace COMExceptions with meaningful ErrorCode with my custom Exceptions in the next manner:

COMException.ErrorCode = -1001 ---> MyLibraryException.ErrorReason = DeviceError.BadFormatOfInputParam && MyLibraryException.InnerException = originalCOMException, where DeviceError is enum & where is definite DeviceError memeber for each particular HRESULT from COM-object.

How can I achieve this using Exception Handling Application Block?


You will need to write a custom exception handler and plug it into the block.

You don't say which version of Entlib you're using. If it's version 5.0 or later, grab the Enterprise Library Extensibility Hands-on labs. The first exercise in on how to write a custom exception handler.

From there you'll just need to implement your logic to do whatever you want.