views:

65

answers:

2

Hi

I have a weird issue. Say you have the following:

Application.ThreadException += something;

try
{
  Application.Run(new Form1());
}
catch (Exception ex)
{

}

Now given an exception happens somewhere in the app, Vista raises the ThreadException event, but XP just jumps straight to the catch block.

How do I get the handling to behave the same? In something I need to handle some cleanup, but it never goes there with XP. Consequently, the FormClosing event never fires from the catch block as all forms are disposed at that stage.

Notes:

  • Both systems run .NET 3.5 SP1, same version
  • Only difference is the OS

Thanks

+2  A: 

You could subscrive to the AppDomain.CurrentDomain.UnhandledException event instead. That's always thrown if an application doesn't handle its exception.

Btw, a try-catch around an Application.Run() statement is considered bad practise as far as I know.

Webleeuw
Nope, I already do that too, does not fire either :(
leppie
I would like not to handle it that way either, but it seems it is the only way to catch (but not handle) them currently in XP (as a last resort).
leppie
That's because you handle the exception within your catch block. Either use an UnhandledException handler OR try-catch around the Application.Run; applying both won't work, at least not under XP :)
Webleeuw
Hmmmm, that sounds fishy, let me try that :)
leppie
HOLY Moses! It works, WTF?? What happened to the element of least surprise???
leppie
+1  A: 

Maybe the UnhandledExceptionMode has a different default value on Vista? Have you tried playing around with this setting? You can explicitly force a mode by calling

// Set the unhandled exception mode to force all Windows Forms errors to go 
// through our handler.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
0xA3
Ahh, saw that ages ago, will see what it does from work tomorrow (+1 in the mean time ;p )
leppie
Ok, that works too! It seems like the better approach.
leppie