The uncatchable exception, pt 2

Posted by chaiguy on Stack Overflow See other posts from Stack Overflow or by chaiguy
Published on 2010-06-16T23:06:56Z Indexed on 2010/06/16 23:12 UTC
Read the original article Hit count: 287

Filed under:
|
|
|
|

Ok I've done some testing and I've reduced the problem to something very simple:

i. Create a method in a new class that throws an exception:

public class Class1 {
    public void CallMe() {
        string blah = null;
        blah.ToLower();
    }
}

ii. Create a MethodInfo that points to this method somewhere else:

Type class1 = typeof( Class1 );
Class1 obj = new Class1();
MethodInfo method = class1.GetMethod( "CallMe" );

iii. Wrap a call to Invoke() in a try/catch block:

try {
    method.Invoke( obj, null ); // exception is not being caught!
} catch {
}

iv. Run the program without the debugger (works fine).

v. Now run the program with the debugger. The debugger will halt the program when the exception occurs, even though it's wrapped in a catch handler that tries to ignore it. (Even if you put a breakpoint in the catch block it will halt before it reaches it!)

In fact, the exception is happening when you run it without the debugger too. In a simple test project it's getting ignored at some other level, but if your app has any kind of global exception handling, it will get triggered there as well.

This is causing me a real headache because it keeps triggering my app's crash-handler, not to mention the pain it is to attempt to debug.

© Stack Overflow or respective owner

Related posts about c#

Related posts about reflection