OnUnhandledException

Topics: CAB & Smart Client Software Factory
Feb 23, 2006 at 4:53 AM
originally posted by: ringman

did you ever find the answer to this? i am also finding the code never fires ... even when not using debugger
Feb 23, 2006 at 11:19 PM
originally posted by: k_savelev

I use

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);

in Main() method

but in SC-BAT Feb 2006 used:

Application.ThreadException += new ThreadExceptionEventHandler(ApplicationThreadException);

for catch exceptions from UI thread
Feb 23, 2006 at 11:20 PM
originally posted by: k_savelev

I try write simple application with use CAB.
Code:

// In MainForm class i have method that fire by click button on the form
// In this method i throw exception

private void button1_Click(object sender, EventArgs e)
{
throw new InvalidOperationException("Oops");
}

// CabeeApplication
public class CabeeApplication : FormShellApplication<WorkItem, MainForm>
{
STAThread
static void Main()
{
// If this line not present OnUnhandledException never fires!
// I try without Debugger attached!
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
new CabeeApplication().Run();
}

public override void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show("Unhadled Exception!!!");
Environment.Exit(0);
}
}
Mar 29, 2006 at 11:35 PM
originally posted by: DingDongLars

I have experienced the same problem and this thread helped me resolve it, thanks.

My application is built on the SC-BAT March drop, but it still had the symptoms more or less as described. Running under the debugger everything worked fine, but outside the debugger, the UI thread exceptions were not caught.

These are the changes I made, prompted by this thread:

STAThread
static void Main() {
//Commented out:
//AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomainUnhandledException);
//(AppDomainUnhandledException never gets called)
// Added:
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);

Application.SetCompatibleTextRenderingDefault(false);

try {
new ShellApplication().Run();
} catch (System.Exception ex) {
HandleException(ex);
}
}

This works, but the original SC-BAT approach did not work, as the assigned handler (AppDomainUnhandledException) never got called.

Any comments from the SC_BAT team are welcome.

Thanks
Lars