Export (0) Print
Expand All

Propagating an Exception

A frequently required exception-handling task is to allow the original exception to propagate up the call stack unchanged. You may want to do this because the handlers only perform actions such as logging that leave the exception unchanged or because other actions, such as wrapping and replacing, have been turned off. For example, a routine within a business logic component may log exceptions at the point where they are detected. It then propagates that exception to the caller for additional handling.

You want to let an exception propagate up the call stack unchanged after the exception handler chain completes. Your application has code in its catch blocks similar to the following. The code does not include definitions of the FormatException and Logger.Log methods. These methods represent typical application code to create and log a formatted exception message.

catch(SomeException e)
    // Exception handling code to be done before propagating.
    string formattedInfo = FormatException(e);
    throw e;

Configure the relevant exception type to use NotifyRethrow as its PostHandlingAction.

The following procedure describes how to use the Exception Handling Block to propagate an exception.

To propagate an exception

  1. Create an exception handling policy with the relevant exception types for your application. For more information, see Entering Configuration Information.
  2. Configure the exception type. Specify the post-handling action as NotifyRethrow.
  3. Modify your application code to call the Process method to execute the target method, as shown here. This code 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 "Propagate Policy." For more information on instantiating objects, see Creating and Referencing Enterprise Library Objects.
    exManager.Process(YourTargetMethod, "Propagate Policy");
  4. Catch the exception in the appropriate section of your application code.

The Process method does not return a value, and therefore you cannot examine the result of the exception handling policy. If you want to be able to make a decision based on the return value from the Exception Handling Application Block, you can use the HandleException method instead.

© 2014 Microsoft