Debug halts on exception when using interception of unity 1.2

Oct 28, 2008 at 10:32 AM
Below is a demonstration of how to reproduce the problem:

1.I have a class called MyClass and it provides a Throw method to throw an ArgumentException, the code is simple
public class MyClass
{
    [MyHandler]
    public virtual void Throw()
    {
        throw new ArgumentException();
    }
}

2.I have a simple CallHandler which catches ArgumentException and convert it to InvalidOperationException, the code
public class MyHandler : ICallHandler
{
    public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        IMethodReturn ret = getNext().Invoke(input, getNext);
        if (ret.Exception != null && ret.Exception.GetType() == typeof(ArgumentException))
        {
            ret.Exception = new InvalidOperationException();
        }
        return ret;
    }

    public int Order { get; set; }
}

3.The corresponding CallHandlerAttribute
public class MyHandlerAttribute : HandlerAttribute
{

    public override ICallHandler CreateHandler(IUnityContainer container)
    {
        MyHandler a = new MyHandler();
        a.Order = Order;
        return a;
    }
}

4.Configure a UnityContainer to put injection to MyClass, the code is also simple
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.Configure<Interception>()
    .SetDefaultInterceptorFor<MyClass>(new VirtualMethodInterceptor());
Note that I use the VirtualMethodInterpctor which generates a child class of my MyClass, maybe it is the problem

5.Resolve the instance and call Throw method
MyClass m = container.Resolve<MyClass>();
try
{
    m.Throw();
}
catch (ArgumentException)
{
    Console.WriteLine("ArgumentException");
}
catch (InvalidOperationException)
{
    Console.WriteLine("InvalidOperationException");
}

Now the problem is:
When it is debug mode, the code will stop at the throw statement in MyClass, showing the exception panel "ArgumentException was unahndled by user code".Throw method, it seems that no explicit try/catch block is around the invocation of Throw method, but in fact I put m.Throw() in a try block and catches both ArgumentException and InvalidOperationException
In release mode the code works well and outputs "InvalidOperationException" as expected

So, why it is halted at the throw statement, it is not a big problem but it does affect my debugging practice, any information on how to resolve it would be appreciated, thanks.





Oct 28, 2008 at 6:18 PM
The throw is handled, the debugger just doesn't realize it. There is a catch block in the generated proxy. It does work, the debugger's just a little confused.

Oct 29, 2008 at 2:23 AM
Yes, it works properly and correctly under release mode, but many times I need to debug the program and the halt is not a convenient thing...