Windows Develop Bookmark and Share   
 index > Windows Forms General > Application.ThreadException and AppDomain.CurrentDomain.UnhandledException events
 

Application.ThreadException and AppDomain.CurrentDomain.UnhandledException events

Hi

My code looks like the following:

static void Main(string[] Args)

{

// Creates an instance of the methods that will handle the exception.

CustomExceptionHandler eh = new CustomExceptionHandler();

// Adds the event handler to to the event.

Application.ThreadException += new ThreadExceptionEventHandler(eh.OnThreadException);

// Set the unhandled exception mode to force all Windows Forms errors to go through

// our handler.

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

// Add the event handler for handling non-UI thread exceptions to the event.

AppDomain.CurrentDomain.UnhandledException +=

new UnhandledExceptionEventHandler(eh.CurrentDomain_UnhandledException);

Form frmMain = new FormMain();

eh.frmMain = frmMain;

Application.Run(frmMain);

}

internal class CustomExceptionHandler

{

public Form frmMain;

// Handles the exception event.

public void OnThreadException(object sender, ThreadExceptionEventArgs t)

{

}

public void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)

{

}

}

As far as i understand, the Application.ThreadException is supposed to catch all the exceptions of the main application thread, however after the Form Main is initializes, none of the exceptions are caught by the Application.ThreadException

also, if the Application.ThreadException doesnt catch the exceptions i would have expected that the AppDomain event would catch them however only few of the exceptions get to the appdomain event whereas many other just stuck the ide.

i am using .net 2005

what am i doing wrong?

Ayala24  Thursday, August 24, 2006 12:32 PM

Hi Ayala24.

The documentation for ThreadException is actually very misleading.  It's easy to infer that this event is actually raised only for exceptions in background threads.  This is not the case, the ThreadException is raised, when the application is appropriately configured, only for exceptions that are throw in the processing of Windows messages (in a single-threaded WinForms application this is for any unhandled exception that occurs after the main form is displayed).

Peter Ritchie  Monday, September 04, 2006 4:35 PM

Hi , you Can see this thread for more help : -

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=239017&SiteID=1

i hope this will help you.

Pr.Wael  Thursday, May 29, 2008 10:24 AM

You can use google to search for other answers

Custom Search

More Threads

• DATAGRID PRINT
• Different toolTip for listBox items
• Need a Good GDI+ Concept
• when to dispose graphics
• Keep MDI child maximized?
• MDI childs and parents
• bug in VS2005beta2
• HTMLDocument.InvokeScript returning null
• converting an integer to a string in C++.NEt
• Chart and Database