How to trap unhandled exceptions
-
Hi. I'm trying to catch all unhandled exceptions in my application (so they can be logged and the user presented with a "nice" message instead of some confusing "computer-y" message. I do the following in my WinForm application:
-- In the constructor // Create the unhandled exception handler AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += new UnhandledExceptionEventHandler(OverAllUnhandledHandler); -- the method static void OverAllUnhandledHandler(object sender, UnhandledExceptionEventArgs args) { Exception e = (Exception) args.ExceptionObject; logger.Log(LogLevel.Fatal, e.ToString()); MessageBox.Show("Please check error logs for details", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
If I create a divide by zero error, it is trapped (either in the constructor or in a button click event. I have even created a thread that starts and causes the error; it is still trapped. However, some errors are still popping up in the standard "Microsoft .NET Framework" dialog, with the text "An unhandled exception has occurred in your application.....". Any idea how to get all unhandled exceptions (I know, code better to catch all exceptions) to be caught by this OverAllUnhandledHandler method? Thanks for any tips. --G -
Hi. I'm trying to catch all unhandled exceptions in my application (so they can be logged and the user presented with a "nice" message instead of some confusing "computer-y" message. I do the following in my WinForm application:
-- In the constructor // Create the unhandled exception handler AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += new UnhandledExceptionEventHandler(OverAllUnhandledHandler); -- the method static void OverAllUnhandledHandler(object sender, UnhandledExceptionEventArgs args) { Exception e = (Exception) args.ExceptionObject; logger.Log(LogLevel.Fatal, e.ToString()); MessageBox.Show("Please check error logs for details", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
If I create a divide by zero error, it is trapped (either in the constructor or in a button click event. I have even created a thread that starts and causes the error; it is still trapped. However, some errors are still popping up in the standard "Microsoft .NET Framework" dialog, with the text "An unhandled exception has occurred in your application.....". Any idea how to get all unhandled exceptions (I know, code better to catch all exceptions) to be caught by this OverAllUnhandledHandler method? Thanks for any tips. --GI put my main code in a try/catch and handle threaded exceptions, it's a long time since my app blew up without logging a nice error for me and telling the user of the same.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
I put my main code in a try/catch and handle threaded exceptions, it's a long time since my app blew up without logging a nice error for me and telling the user of the same.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Thanks -- haven't been handling threaded exceptions -- giving that a shot. Do you mean you put a try/catch around the
Application.Run(formName);
inMain()
? -
Thanks -- haven't been handling threaded exceptions -- giving that a shot. Do you mean you put a try/catch around the
Application.Run(formName);
inMain()
?Yes, that's all I did initially, then as I am multithreaded, I had to add code to handle exceptions in threads.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Hi. I'm trying to catch all unhandled exceptions in my application (so they can be logged and the user presented with a "nice" message instead of some confusing "computer-y" message. I do the following in my WinForm application:
-- In the constructor // Create the unhandled exception handler AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += new UnhandledExceptionEventHandler(OverAllUnhandledHandler); -- the method static void OverAllUnhandledHandler(object sender, UnhandledExceptionEventArgs args) { Exception e = (Exception) args.ExceptionObject; logger.Log(LogLevel.Fatal, e.ToString()); MessageBox.Show("Please check error logs for details", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
If I create a divide by zero error, it is trapped (either in the constructor or in a button click event. I have even created a thread that starts and causes the error; it is still trapped. However, some errors are still popping up in the standard "Microsoft .NET Framework" dialog, with the text "An unhandled exception has occurred in your application.....". Any idea how to get all unhandled exceptions (I know, code better to catch all exceptions) to be caught by this OverAllUnhandledHandler method? Thanks for any tips. --GI handle exceptions using: AppDomain.UnhandledException, System.Windows.Forms.Application.ThreadException, and a catch-all handler in void Main(). This gets all unhandled exceptions (well, except for some that are swallowed by Windows Forms/COM in the Drag* events - put catch handlers in all Drag* event handlers).
-
Yes, that's all I did initially, then as I am multithreaded, I had to add code to handle exceptions in threads.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Great. Thanks. I've included both the try/catch and the ThreadException code -- we'll see how it runs this afternoon!
-
I handle exceptions using: AppDomain.UnhandledException, System.Windows.Forms.Application.ThreadException, and a catch-all handler in void Main(). This gets all unhandled exceptions (well, except for some that are swallowed by Windows Forms/COM in the Drag* events - put catch handlers in all Drag* event handlers).
Thanks Daniel -- this is now how the application works (I previously only had AppDomain.UnhandledException, thinking that it would get all unhandled exceptions). Since I don't (currently) do any drag events, I should be OK (but I'll keep it in mind for future projects). --G