程序出现错误时可采用如下错误处理机制:出错时跳到标签处,判断错误类型修正错误,重新返回正确标签处,继续运行程序。
代码如下:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
On Error GoTo errHandler
Dim testClass As Boolean
testClass = "Hello,world!"
programeHandler:
MsgBox("程序正常运行!")
Exit Sub
errHandler:
MsgBox("程序出现错误!")
MsgBox("错误号为:" & Err.Number)
MsgBox("错误描述:" & Err.Description)
If Err.Number = 13 Then
testClass = True
MsgBox(“错误被修正”)
Resume programeHandler
End If
End Sub
运行结果如下: