Application.SetUnhandledExceptionMode Method (UnhandledExceptionMode)
Instructs the application how to respond to unhandled exceptions.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
You cannot set the exception mode after the application has created its first window.
It is often not feasible to catch all of the exceptions thrown by Windows Forms. Using this method, you can instruct your application whether it should catch all unhandled exceptions thrown by Windows Forms components and continue operating, or whether it should expose them to the user and halt execution.
To catch exceptions that occur in threads not created and owned by Windows Forms, use the UnhandledException event handler.
The following code example sets event handlers for exceptions that occur on Windows Forms threads and exceptions that occur on other threads. It sets SetUnhandledExceptionMode so that all exceptions are handled by the application, regardless of the settings in the application's user configuration file. It uses the ThreadException event to handle UI thread exceptions, and the UnhandledException event to handle non-UI thread exceptions. Since UnhandledException cannot prevent an application from terminating, the example simply logs the error in the application event log before termination.