PublicClass mainClass main PublicSharedSub main()Sub main() '定义出错机制线程 '其实就是一个多线程,在程序运行过程中,此 AddHandler Application.ThreadException, AddressOf OnThreadException Application.EnableVisualStyles() Application.Run(New Formglobal) End Sub ThreadException#Region "ThreadException" ' Handles the exception event. PublicSharedSub OnThreadException()Sub OnThreadException(ByVal sender AsObject, ByVal t As ThreadExceptionEventArgs) Dim result As DialogResult = System.Windows.Forms.DialogResult.Cancel Try result = ShowThreadExceptionDialog(t.Exception) Catch Try MessageBox.Show("Fatal Error", "Fatal Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop) Finally Application.Exit() EndTry EndTry ' Exits the program when the user clicks Abort. If (result = System.Windows.Forms.DialogResult.Abort) Then Application.Exit() EndIf End Sub ' Creates the error message and displays it. PrivateSharedFunction ShowThreadExceptionDialog()Function ShowThreadExceptionDialog(ByVal e As Exception) As DialogResult Dim errorMsg As StringWriter =New StringWriter errorMsg.WriteLine("------------ 有错误发生 ------------:") errorMsg.WriteLine("") IfTypeOf e Is Facet_Demo.FacetException Then errorMsg.WriteLine(CType(e, FacetException).ErrorMessage) Else errorMsg.WriteLine(e.Message) EndIf errorMsg.WriteLine("------------------------------------") 'errorMsg.WriteLine("Stack Trace:") 'errorMsg.WriteLine(e.StackTrace) Return MessageBox.Show(errorMsg.ToString(), "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Function #End Region End Class