关于Application.ThreadException和AppDomain.UnhandledException
Posted on 2005-10-26 17:09 水如烟(LzmTW) 阅读(1706) 评论(0) 编辑 收藏 举报果是奇怪
参考:风满袖《To handle Unhandled Exception 》
Public Class Form2
Inherits System.Windows.Forms.Form
Windows 窗体设计器生成的代码
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ThrowException.ThrowAppDomainExcetion()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ThrowException.ThrowThreadExcetion()
End Sub
End Class
Inherits System.Windows.Forms.Form
Windows 窗体设计器生成的代码
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ThrowException.ThrowAppDomainExcetion()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ThrowException.ThrowThreadExcetion()
End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
Windows 窗体设计器生成的代码
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
Dim frm As New Form2
frm.MdiParent = Me
frm.Show()
End Sub
End Class
Inherits System.Windows.Forms.Form
Windows 窗体设计器生成的代码
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
Dim frm As New Form2
frm.MdiParent = Me
frm.Show()
End Sub
End Class
Public Class ThrowException
Public Shared Sub ThrowAppDomainExcetion()
'引发异常
AppDomain.CurrentDomain.Unload(AppDomain.CurrentDomain)
End Sub
Public Shared Sub ThrowThreadExcetion()
Throw New Exception("ThrowThreadExcetion")
End Sub
End Class
Public Class App
Shared 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 AcceptExceptionHandler
Public Shared 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(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(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
Public Shared Sub ThrowAppDomainExcetion()
'引发异常
AppDomain.CurrentDomain.Unload(AppDomain.CurrentDomain)
End Sub
Public Shared Sub ThrowThreadExcetion()
Throw New Exception("ThrowThreadExcetion")
End Sub
End Class
Public Class App
Shared 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 AcceptExceptionHandler
Public Shared 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(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(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