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


i have 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 "throw" in catch standard winforms "unhandled exception" dialog never shows up.

thismethodthrowsanexception() throwing exception of type system.exception. have event attached application.threadexception. event not being hit in case. uibutton standard winforms button. created button throws exception in event handler , exception is being caught application_threadexception. of processing happening on 1 thread.

for life of me can't see why code wouldn't result in normal exception box (which, since app use, that's helpful). there other places in app i've seen standard exception dialog.

i have button click handler looks this:

 private void button2_click(object sender, eventargs e)     {         throw new exception("i broke!");      } 

and threadexception handler looks this:

 static void application_threadexception(object sender, threadexceptioneventargs e)         {             messagebox.show("exception: " + e.exception.message);          } 

i'm using unhandledexceptionmode.automatic. when click uibutton1 not exception message via application_threadexception. if add throw new exception before method call thismethodthrowsanexception, messagebox.

i messagebox via button2 (through application_threadexception).

so happening in thismethodthrowsanexception. thing can think of it's starting new thread, that's not case when debug through it.

if exception happening on different thread catch block in uibutton1_click?

the debugger getting in way. application class aware debugger attached program, uses system.diagnostics.debugger.isattached(). if that's case, not use try/catch block around message loop , application.threadexception event not run.

this quite intentional. if catch block active, have hard time troubleshooting exceptions in program. without catch block, debugger stop , show wrong.

you can reproduce runtime behavior. add line of code main() method before run() call:

 application.setunhandledexceptionmode(unhandledexceptionmode.catchexception); 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -