how do you catch unhandle exception in .net?
how do you catch unhandle exception in .net?
Collapse
X
-
Tags: None
-
Hi!
I have done this once in C#, VS2008.
Below is the file program.cs. There is an exception handler for thread exception and another one for unhandled exception. The code is tested, exceptions actually cathed by this code. After the exception, application is terminated.
Code:using System; using System.Windows.Forms; namespace GameSelector { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {//In this function you put your exception code, below is the code I wrote for the exception GameSelector.MainForm.sss = GameSelector.MainForm.unh_exc; GameSelector.MainForm.unh_exc = "unhexc"; try { GameSelector.MainForm.unlock_all__(); Exception ex = (Exception)e.ExceptionObject;//declare exception //Important message box, tells you the serial number of exception (unh_exc) MessageBox.Show(GameSelector.MainForm.unh_exc + GameSelector.MainForm.sss + " information:\n\n" + ex.Message + ex.StackTrace, "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); } finally { Application.Exit(); } } public static void Application_ThreadException (object sender, System.Threading.ThreadExceptionEventArgs e) {//same for thread exception GameSelector.MainForm.sss = GameSelector.MainForm.unh_exc; GameSelector.MainForm.unh_exc = "unhexc"; try { GameSelector.MainForm.unlock_all__(); //Exception ex = (Exception)e.ExceptionObject; //result = MessageBox.Show(GameSelector.MainForm.unh_exc + GameSelector.MainForm.sss + e.Exception.Message + e.Exception.StackTrace, "Application Error"); //, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop); /*MessageBox.Show(GameSelector.MainForm.unh_exc + GameSelector.MainForm.sss + " information:\n\n" + ex.Message + ex.StackTrace, "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);*/ } finally { Application.Exit(); } } } }
Hope that helped,
Alexis.
Comment