If you mean handling the Application.ThreadException event: it fires only for exceptions that were selected from WinForms threads. Typically, an application has one WinForms thread: the main thread.
UPDATE
Here's an example showing the difference in the behavior of Application.ThreadException and AppDomain.UnhandledException:
1) Program class:
static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.Run(new Form1()); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Debug.WriteLine(Thread.CurrentThread.Name); } static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { Debug.WriteLine(Thread.CurrentThread.Name); } }
2) The main form (form with two buttons) code-behind:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { throw new InvalidOperationException(); } private void button2_Click(object sender, EventArgs e) { new Thread(() => { throw new InvalidOperationException(); }) { Name = "Worker Thread" }.Start(); } }
When you click on button1, you throw an exception from the WinForms thread. Thus, this exception will be handled by default Application_ThreadException .
When you click button2, you throw an exception to the workflow, which is not a WinForms thread. Application.ThreadException does not fire in this case; instead, the AppDomain.UnhandledException event is CurrentDomain_UnhandledException (and a CurrentDomain_UnhandledException is CurrentDomain_UnhandledException , producing the "Workflow" line in the output window).
source share