Saturday, September 29, 2012

Can't Catch Exceptions When Invoking Methods Via Reflection in .NET 4

I just tried updating the Sasa build to .NET 4, when I ran into a bizarre problem. Basically, the following code throws an exception when running under .NET 4 that isn't caught by a general exception handler:

static int ThrowError()
{
    throw new InvalidOperationException(); // debugger breaks here
}
public static void Main(string[] args)
{
    var ethrow = new Func<int>(ThrowError).Method;
    try
    {
        ethrow.Invoke(null, null);
    }
    catch (Exception)
    {
        // general exception handler doesn't work
    }
}

Turns out this is a Visual Studio setting. Given the description there, whatever hooks VS has into the runtime when transitioning between the native and managed code have changed their behaviour from .NET 3.5. So VS isn't aware of the general exception handler further up the stack, and breaks immediately.

I can see this being handy if you're writing reflection-heavy code, as it breaks at the actual source of an exception instead of breaking at the dynamic method invocation as it would under .NET 3.5. It's just annoying when you are already handling it.