Partager via


Replacing an Exception

A frequently required exception-handling task is to replace the original exception with another exception. For example, if an exception is going to cross a trust boundary, you may not want to send the original exception because it contains sensitive information.

Typical Goals

You want to replace the original exception, which contains sensitive or unnecessary information, with another that is more appropriate. Your application has code in its catch blocks similar to the following.

catch(SomeException e)
{
  CustomException myException = new CustomException("Unable to access the configured data source");
  throw myException;
}
'Usage
Catch e As SomeException
  Dim myException As CustomException = New CustomException("Unable to access the configured data source")
  Throw myException
End Try

Solution

Use the replace handler in your exception handling chain. Execute the policy before propagating the exception out of the application layer.

Using the Replace Handler

The following procedure describes how to use the replace handler.

To use the replace handler

  1. Create an exception handling policy with the appropriate exception types for your application. For more information, see Entering Configuration Information.

  2. Configure the exception type, specifying the post-handling action as ThrowNewException. This causes the block to throw the new exception that has been created by replacing the original exception. The throw occurs after the entire chain of handlers runs.

  3. Add a new replace handler for the specified exception types.

  4. Configure the replace handler. Add the exception message to be used and the exception type that will be used to replace the originating exception.

  5. Modify your application code to execute the new policy when an exception occurs. The following code shows how to do this. It assumes you have resolved an instance of the ExceptionManager class through the Enterprise Library container and saved it in a variable named exManager. Replace the name "Replace Policy" with the name of your own replacement policy. For more information on instantiating objects, see Creating and Referencing Enterprise Library Objects.

    exManager.Process(YourTargetMethod, "Replace Policy");
    
    'Usage
    exManager.Process(AddressOf YourTargetMethod, "Replace Policy")
    

Usage Notes

Consider the following information when you configure the block to use a replace handler:

  • Typically, exceptions are replaced after they are logged in their original state.
  • To help in the management and tracking of exceptions, the block generates a HandlingInstanceId that you can use to map to the originating 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.
  • The exception that the block throws is the final exception that results after the entire chain of handlers runs. For example, if the chain specifies that exception A is to be replaced with exception B, and a later handler replaces exception B with exception C, exception C will be thrown.
  • The constructor for the replace handler accepts only one argument of type string. This means that you must provide this constructor overload for all custom exception types that are used to replace other exceptions.