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