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

沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...