tags:

views:

62

answers:

1

I have this code:

001 private void uiButton1_Click(object sender, EventArgs e)
002 {
003     string someString = "";
004     try
005     {
006         someString = ThisMethodThrowsAnException();
007     }
008     catch (Exception)
009     {        
010         throw;
011     }
012 }

The code does get to the "throw" in the catch but the standard winforms "unhandled exception" dialog never shows up.

ThisMethodThrowsAnException() is throwing an exception of type System.Exception. I have an event attached to Application.ThreadException. That event is not being hit in this case. uiButton is a standard winforms button. I created a button that throws an exception in its event handler and that exception is being caught by Application_ThreadException. All of the processing is happening on one thread.

For the life of me I can't see why this code wouldn't result in the normal exception box (which, since this is an app that only I use, is something that's helpful). There are other places in this app where I've seen the standard exception dialog.

I have a button click handler that looks like this:

 private void button2_Click(object sender, EventArgs e)
    {
        throw new Exception("I are broke!"); 
    }

And a ThreadException handler that looks like this:

 static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            MessageBox.Show("Exception: " + e.Exception.Message); 
        }

I'm using UnhandledExceptionMode.Automatic. When I click the uiButton1 I do not get an exception message via Application_ThreadException. However if I add a throw new Exception just before the method call to ThisMethodThrowsAnException, I do get the MessageBox.

I do get a MessageBox via button2 (through Application_ThreadException).

So something is happening in ThisMethodThrowsAnException. The only thing I can think of is that it's starting up a new thread, but that's not the case when I debug through it.

If the exception was happening on a different thread would it get back to the catch block in uiButton1_Click?

+1  A: 

The debugger is getting in the way. The Application class is aware that a debugger is attached to the program, it uses System.Diagnostics.Debugger.IsAttached(). If that's the case, it does not use a try/catch block around the message loop and the Application.ThreadException event will not run.

This is quite intentional. If the catch block were active, you would have a very hard time troubleshooting exceptions in your program. Without the catch block, the debugger will always stop and show you what is wrong.

You can reproduce the runtime behavior. Add this line of code to your Main() method before the Run() call:

 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Hans Passant
Ahh, but nowhere did I say that I only saw this behavior while a debugger was attached. I tried all three app exception modes. The results: two gave me no exception dialog and the third closed my app ("MyApp has encountered a problem and needs to close.")
jcollum
To clarify: while running the app without the debugger, the uiButton1_Click simply does nothing. While running with the debugger the debugger stops on line 10.
jcollum
Your ThreadException event handler appears to be gobbling exceptions without reporting them.
Hans Passant
@nobugz: don't think that's the case. Added more text to the question to clarify.
jcollum
This one wins by default.
jcollum