Using ExceptionPolicy.HandleException with exceptionToThrow overload

Topics: Exception Handling Application Block
May 16, 2008 at 7:30 PM
I'm attempting to use the overload of ExceptionPolicy.HandleException(Exception exceptionToHandle, string policyName, out Exception exceptionToThrow).
I never get anything back in exceptionToThrow. It appears that it is hardcoded to null (looking at the source code in ExceptionPolicy.cs).

I'm using a WrapHandler to wrap an exception with another and I want to be able to throw the wrapped exception from my catch handler so that the stack trace is correct.
My catch statement is as follows:

                catch (Exception e)
                    Exception exceptionToThrow;
                    if (ExceptionPolicy.HandleException(e, "Wrap Policy", out exceptionToThrow))
                        if (exceptionToThrow != null) 
                            throw exceptionToThrow;

It never hits the "throw exceptionToThrow" because exceptionToThrow is always null.
My "Wrap Policy" is set to postHandlingAction="NotifyRethrow" so that the Application block does not attempt to throw the wrapped exception itself.

Is this the correct way to do what I'm trying to accomplish?



May 16, 2008 at 7:57 PM
I answered my own question.

It turns out that you have to use postHandlingAction="ThrowNewException".
This works because the exception is caught within the Application Block and returned in the out parameter correctly.