简体   繁体   中英

why would a re-thrown exception in a button click not get back to the user?

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM