Shared Sub main()Sub main()
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf AcceptExceptionHandler.OnUnhandledAppDomainException
AddHandler Application.ThreadException, AddressOf AcceptExceptionHandler.OnUnhandledThreadException
Application.Run(New Form1)
End Sub
End Class
Public Class AcceptExceptionHandlerClass AcceptExceptionHandler
Public Shared Sub OnUnhandledAppDomainException()Sub OnUnhandledAppDomainException(ByVal sender As Object, ByVal t As System.UnhandledExceptionEventArgs)
Dim result As DialogResult = System.Windows.Forms.DialogResult.Cancel
Try
result = ShowExceptionDialog("程序域异常 终止:" & t.IsTerminating.TrueString, DirectCast(t.ExceptionObject, Exception))
Catch
Try
MessageBox.Show("程序域异常", "错误", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop)
Finally
Application.Exit()
End Try
End Try
If (result = System.Windows.Forms.DialogResult.Abort) Then
Application.Exit()
End If
End Sub
Public Shared Sub OnUnhandledThreadException()Sub OnUnhandledThreadException(ByVal sender As Object, ByVal t As System.Threading.ThreadExceptionEventArgs)
Dim result As DialogResult = System.Windows.Forms.DialogResult.Cancel
Try
result = ShowExceptionDialog("线程异常", t.Exception)
Catch
Try
MessageBox.Show("线程异常", "错误", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop)
Finally
Application.Exit()
End Try
End Try
If (result = System.Windows.Forms.DialogResult.Abort) Then
Application.Exit()
End If
End Sub
Private Shared Function ShowExceptionDialog()Function ShowExceptionDialog(ByVal s As String, ByVal e As Exception) As DialogResult
Dim errorMsg As System.IO.StringWriter = New System.IO.StringWriter
errorMsg.WriteLine(s & ",信息如下:")
errorMsg.WriteLine("")
errorMsg.WriteLine(e.Message)
errorMsg.WriteLine("")
errorMsg.WriteLine("栈:")
errorMsg.WriteLine(e.StackTrace)
Return MessageBox.Show(errorMsg.ToString(), "警告", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop)
End Function
End Class