>[STAThread] >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); > Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); > > Application.Run(new MainForm()); >} > >// Creates a class to handle the exception event. >internal class CustomExceptionHandler >{ > // Handles the exception event. > public void OnThreadException(object sender, ThreadExceptionEventArgs t) > { > DialogResult result = this.ShowThreadExceptionDialog(t.Exception); > > // Exits the program after displaying message to the user. > // In Development mode, the developer will have more options (Abort/Retry/Ignore). > if (result == DialogResult.OK || result == DialogResult.Abort) > Application.Exit(); > } > > // Creates the error message and displays it. > private DialogResult ShowThreadExceptionDialog(Exception e) > { > DialogResult retval; > string msgUser = "An error occurred please contact the adminstrator with the following information:\n\n"; > string msgDev = "An unhandled exception occurred (Abort/Retry/Ignore buttons are only displayed to Developers) \n\n"; > > string msgTrace = "Error: " + e.Message + "\n\n"; > if (e.InnerException != null) > msgTrace += " " + e.InnerException.Message + "\n\n"; > msgTrace += "Error Method: " + e.TargetSite + "\n\n" + > "Stack Trace: " + e.StackTrace; > > if (System.Diagnostics.Debugger.IsAttached) > retval = MessageBox.Show(msgDev + msgTrace, "Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Exclamation); > else > retval = MessageBox.Show(msgUser + msgTrace, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); > > return retval; > } >} >>