现在的位置: 首页 > 综合 > 正文

关于Application.ThreadException和AppDomain.UnhandledException

2011年08月11日 ⁄ 综合 ⁄ 共 6665字 ⁄ 字号 评论关闭

果是奇怪

参考:风满袖《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

 

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

 

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 ObjectByVal 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 ObjectByVal 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 
【上篇】
【下篇】

抱歉!评论已关闭.