I am trying to catch an exception in a legacy winforms applications but somehow the exception is always captured by the global exception handling.
In the Program.cs I have the following code :
Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.ThreadException += Application_ThreadException; Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { //throw new NotImplementedException(); MessageBox.Show(e.Exception.Message); } and in the form I have the following code:
private void gttBindingSourceLosplaatsen_PositionChanged(object sender, EventArgs e) { try { bool enabled = ((DataTable)gttBindingSourceLosplaatsen.DataSource).Rows.Count > 0; buttonEditLosplaats.Enabled = enabled; buttonDeleteLosplaats.Enabled = enabled; } catch { MessageBox.Show("what is this ?"); } } Now when the line "buttonDeleteLosplaats.Enable = enabled;" is executed an exception occurs. Now I expect to fall into the catch block and show the message "What is this ?" but that does not happen, I always fall into the global exception handler instead.
What could cause this ? There are a lot of other try..catch constructs in this application that do work as I expect and do fall into the catch block, but not this one. Does anybody has an idea why I do not fall into the local try...catch block but into the global exception handler ?
ShowtoMessageBox.Show(e.ToString())then post the result.catch (Excetion e)?AppDomain.CurrentDomain.UnhandledExceptionandmainitself (its code should reside insidetry/catchas well).Index -1 does not have a valuetells us the exception occurred somewhere else after you set the button the enabled, and that somewhere else most likely doesn't catch the exception.