Sending an Exception to the Exception Handling Application Block

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.

Interaction between application code and the Exception Handling Application Block occurs when the application code catches an exception and sends it to the application block to be handled. Application developers do not have to know how exceptions will be handled because they have to specify only the name of the relevant exception policy.

The following code shows how to pass exceptions of a custom type named DataAccessException to the Exception Handling Application Block. In the example, this causes a policy named Data Access Policy to be applied. The code checks the true or false value returned by the HandleException method. If the value is true, the original exception is rethrown. All other exception types are propagated back to the calling code.

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

Note

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. This class exposes the same API as the ExceptionPolicy class static façade. For more information about using the Unity Application Block to create and inject instances of Enterprise Library objects, see Creating Objects Using the Unity Application Block.