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.

Typical Goals

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.

Note

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);
    Logger.Log(e);
    throw e;
}
'Usage
Catch e As SomeException
  ' Exception handling code to be done before propagating.
  Dim formattedInfo As String = FormatException(e)
  Logger.Log(e)
  Throw e
End Try

QuickStart

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.

Solution

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

Propagating an Exception

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.

    try
    {
      // Run code.
    }
    catch(Exception ex)
    {
      bool rethrow = ExceptionPolicy.HandleException(ex, "Propagate Policy");
      if (rethrow)
      {
        // Throw original exception.
        throw;
      }
    }
    
    'Usage
    Try
      ' Run code.
    Catch ex As Exception
      Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Propagate Policy")
      If (rethrow) Then
        ' Throw original exception.
        Throw
      End If
    End Try
    

Usage Notes

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.