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.
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. 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);
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
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
Create an exception handling policy with the relevant exception types for your application. For more information, see Entering Configuration Information.
Configure the exception type. Specify the post-handling action as NotifyRethrow.
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");
'Usage exManager.Process(AddressOf YourTargetMethod, "Propagate Policy")
Catch the exception in the appropriate section of your application code.
Usage Notes
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.