Application.SetUnhandledExceptionMode Method (UnhandledExceptionMode)
Instructs the application how to respond to unhandled exceptions.
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
'Declaration Public Shared Sub SetUnhandledExceptionMode ( _ mode As UnhandledExceptionMode _ )
Parameters
- mode
- Type: System.Windows.Forms.UnhandledExceptionMode
An UnhandledExceptionMode value describing how the application should behave if an exception is thrown without being caught.
| Exception | Condition |
|---|---|
| InvalidOperationException | 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.
Call SetUnhandledExceptionMode before you instantiate the main form of your application using the Run method.
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.
This example assumes that you have defined two Button controls, button1 and button2, on your Form class.
Private newThread As Thread = Nothing ' Starts the application. <SecurityPermission(SecurityAction.Demand, Flags:=SecurityPermissionFlag.ControlAppDomain)> _ Public Shared Sub Main() ' Add the event handler for handling UI thread exceptions to the event. AddHandler Application.ThreadException, AddressOf ErrorHandlerForm.Form1_UIThreadException ' Set the unhandled exception mode to force all Windows Forms errors to go through ' our handler. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException) ' Add the event handler for handling non-UI thread exceptions to the event. AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf CurrentDomain_UnhandledException ' Runs the application. Application.Run(New ErrorHandlerForm()) End Sub ' Programs the button to throw an exception when clicked. Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click Throw New ArgumentException("The parameter was invalid") End Sub ' Start a new thread, separate from Windows Forms, that will throw an exception. Private Sub button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button2.Click Dim newThreadStart As New ThreadStart(AddressOf newThread_Execute) newThread = New Thread(newThreadStart) newThread.Start() End Sub ' The thread we start up to demonstrate non-UI exception handling. Sub newThread_Execute() Throw New Exception("The method or operation is not implemented.") End Sub ' Handle the UI exceptions by showing a dialog box, and asking the user whether ' or not they wish to abort execution. Private Shared Sub Form1_UIThreadException(ByVal sender As Object, ByVal t As ThreadExceptionEventArgs) Dim result As System.Windows.Forms.DialogResult = _ System.Windows.Forms.DialogResult.Cancel Try result = ShowThreadExceptionDialog("Windows Forms Error", t.Exception) Catch Try MessageBox.Show("Fatal Windows Forms Error", _ "Fatal Windows Forms Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop) Finally Application.Exit() End Try End Try ' Exits the program when the user clicks Abort. If result = DialogResult.Abort Then Application.Exit() End If End Sub ' Handle the UI exceptions by showing a dialog box, and asking the user whether ' or not they wish to abort execution. ' NOTE: This exception cannot be kept from terminating the application - it can only ' log the event, and inform the user about it. Private Shared Sub CurrentDomain_UnhandledException(ByVal sender As Object, _ ByVal e As UnhandledExceptionEventArgs) Try Dim ex As Exception = CType(e.ExceptionObject, Exception) Dim errorMsg As String = "An application error occurred. Please contact the adminstrator " & _ "with the following information:" & ControlChars.Lf & ControlChars.Lf ' Since we can't prevent the app from terminating, log this to the event log. If (Not EventLog.SourceExists("ThreadException")) Then EventLog.CreateEventSource("ThreadException", "Application") End If ' Create an EventLog instance and assign its source. Dim myLog As New EventLog() myLog.Source = "ThreadException" myLog.WriteEntry((errorMsg + ex.Message & ControlChars.Lf & ControlChars.Lf & _ "Stack Trace:" & ControlChars.Lf & ex.StackTrace)) Catch exc As Exception Try MessageBox.Show("Fatal Non-UI Error", "Fatal Non-UI Error. Could not write the error to the event log. " & _ "Reason: " & exc.Message, MessageBoxButtons.OK, MessageBoxIcon.Stop) Finally Application.Exit() End Try End Try End Sub ' Creates the error message and displays it. Private Shared Function ShowThreadExceptionDialog(ByVal title As String, ByVal e As Exception) As DialogResult Dim errorMsg As String = "An application error occurred. Please contact the adminstrator " & _ "with the following information:" & ControlChars.Lf & ControlChars.Lf errorMsg = errorMsg & e.Message & ControlChars.Lf & _ ControlChars.Lf & "Stack Trace:" & ControlChars.Lf & e.StackTrace Return MessageBox.Show(errorMsg, title, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop) End Function
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.