|
顺其自然,水到渠成 LzmTW
文或代码皆是面向初学者.我是爱好者,也是初学者.那些"文章",只按自己理解写,我是不知术语名词的.所以只供参考,也仅供参考.
|
posts - 88, comments - 820, trackbacks - 1, articles - 0
|
|
|
Posted on 2005-10-26 17:09 水如烟(LzmTW) 阅读(677) 评论(0) 编辑 收藏
果是奇怪
参考:风满袖《To handle Unhandled Exception 》
 Public Class Form2Class Form2
Inherits System.Windows.Forms.Form

 Windows 窗体设计器生成的代码#Region " Windows 窗体设计器生成的代码 "

 Public Sub New()Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
 Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(56, 72)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(136, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(56, 128)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(136, 32)
Me.Button2.TabIndex = 1
Me.Button2.Text = "Button2"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)

End Sub

#End Region

 Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ThrowException.ThrowAppDomainExcetion()
End Sub

 Private Sub Button2_Click()Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ThrowException.ThrowThreadExcetion()
End Sub
End Class

 Public Class Form1Class Form1
Inherits System.Windows.Forms.Form

 Windows 窗体设计器生成的代码#Region " Windows 窗体设计器生成的代码 "

 Public Sub New()Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
 Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.MenuItem1 = New System.Windows.Forms.MenuItem
Me.MenuItem2 = New System.Windows.Forms.MenuItem
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
'
'MenuItem1
'
Me.MenuItem1.Index = 0
Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2})
Me.MenuItem1.Text = "File"
'
'MenuItem2
'
Me.MenuItem2.Index = 0
Me.MenuItem2.Text = "Test"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(488, 245)
Me.IsMdiContainer = True
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.Text = "Form1"

End Sub

#End Region

 Private Sub MenuItem2_Click()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 ThrowExceptionClass ThrowException

 Public Shared Sub ThrowAppDomainExcetion()Sub ThrowAppDomainExcetion()
'引发异常
AppDomain.CurrentDomain.Unload(AppDomain.CurrentDomain)
End Sub

 Public Shared Sub ThrowThreadExcetion()Sub ThrowThreadExcetion()
Throw New Exception("ThrowThreadExcetion")
End Sub

End Class

 Public Class AppClass App

 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

|