Propagating an Exception

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

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 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;

For an extended example of how to use the Exception Handling Application Block to propagate the original exception, see the QuickStart walkthrough, Walkthrough: Propagating the Original Exception.

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 PostHandlingAction as NotifyRethrow. This causes the application block to return true to the application from the call to the HandleException method.
  3. Modify your application code to execute the new policy when an exception occurs.
  4. Examine the return value from the HandleException call. If the return value is true, execute a throw statement, as shown in the following code example.
      // Run code.
    catch(Exception ex)
      bool rethrow = ExceptionPolicy.HandleException(ex, "Propagate Policy");
      if (rethrow)
        // Throw original exception.

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.