I am wrapping the ExceptionManager in Microsoft's Enterprise Library's Exception Handling block, and I want to allow for consumers of my wrapper to specify which exception types are allowed to be rethrown by instantiators of the wrapper. Like so:
public class MyExceptionManager<T> : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager where T: MyCustomException
{
...
public void LogWrapAndRethrow(Exception e, T t)
{
base.HandleException( e, "MyPolicyName", out t);
}
...
}
I'd like for users to be able to make:
var em = new MyExceptionManager<MyCustomExceptionClass>();
var x = new MyCustomExceptionClass();
em.LogWrapAndRethrow(new Exception(),x);
...and have it checked at compile time
Here is the signature of the base method:
public bool HandleException(Exception exceptionToHandle, string policyName, out Exception exceptionToThrow);
It's not a generic class.
Unfortunately, I keep getting "the best overloaded match for... has some invalid arguments", even though T is constrained to a type that inherits from Exception. If I manually create and assign an Exception variable, it works as an output parameter.
What am I doing wrong?
The argument you use for an out
parameter has to exactly match the declared parameter type. As HandleException
has a third parameter out Exception exceptionToThrow
, that won't work at the moment.
Given that you don't need the exception to come back out of your method, it's simplest to write the method as:
public void LogWrapAndRethrow(Exception e, T t)
{
Exception tmp = t;
base.HandleException( e, "MyPolicyName", out tmp);
}
Now you're calling the method with an argument of type Exception
, so that's okay.
See more on this question at Stackoverflow