2017年11月22日 星期三

[ VB2010 ] - 014 錯誤補捉流程

[ VB2010 ] - 014 錯誤補捉流程

●發生錯誤時則忽視:
●發生錯誤時則跳躍至指定位置:
●發生錯誤時則跳躍至錯誤捕捉區塊內:
●自行觸發錯誤:



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

沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...