program.cs
static class Program { [STAThread] static void Main() { Application.ThreadException += Application_ThreadException; Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { MessageBox.Show(e.Exception.ToString()); } }
Form1.cs
public partial class Form1 : Form { public Form1() { InitializeComponent(); this.ContextMenuStrip = contextMenuStrip1; } private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e) { throw new Exception("Closed Exception"); } }
1. Build and Run (Ctrl + F5).
2. Mouse right button click on Form.
3. Show ContextMenu and Form Click (not ContextMenu).
4. Raise unhandled event and application kill.
Why not fire Application.ThreadException event?
But.
3. Show ContextMenu and ContextMenuItem Click
4. Fire Application.ThreadException event and keep appliation.