●發生錯誤時則忽視:
●發生錯誤時則跳躍至指定位置:
●發生錯誤時則跳躍至錯誤捕捉區塊內:
●自行觸發錯誤:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
On Error Resume Next
Dim A As String = "X"
TextBox1.Text = ""
A = 1 / A
If Err.Number <> 0 Then
TextBox1.Text &= Err.Description + vbLf
Err.Clear()
Else
TextBox1.Text &= A + vbLf
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
On Error GoTo ExceptionHandle
Dim A As String = "X"
TextBox1.Text = ""
A = 1 / A
TextBox1.Text &= A + vbLf
Exit Sub
ExceptionHandle:
TextBox1.Text &= Err.Description + vbLf
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim A As String = "X"
TextBox1.Text = ""
Try
A = 1 / A
TextBox1.Text &= A + vbLf
Catch
TextBox1.Text &= Err.Description + vbLf
End Try
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
On Error GoTo Exception
Dim A As Int32
TextBox1.Text = ""
If A = 0 Then
Err.Raise(-1, , "Sorry! It's my fault")
Else
TextBox1.Text &= 1 / A + vbLf
End If
Exit Sub
Exception:
TextBox1.Text &= Err.Description + vbLf
End Sub
End Class
沒有留言:
張貼留言