在子函数和母函数处理错误时,有两个问题值得讨论:1)子函数的Err变量信息是否传回给母函数;2)子函数的错误是否会触发母函数的错误处理程序。 结合MSDN相关文档和各种测试案例,我发现:
第四种情况子函数错误触发母函数的错误处理程序的原因是当子函数的 下面这几个测试案例展示了上述行为特征: Sub Demo4() On Error GoTo err_hander1 Call Demo4_Sub1 Debug.Print "Err in sub1:" & Err.Description ' Err description = "" On Error GoTo err_hander2 Call Demo4_Sub2 Debug.Print "Err in sub2:" & Err.Description ' Err description <> "" On Error GoTo err_hander3 Call Demo4_Sub3 Debug.Print "Err in sub3:" & Err.Description ' Err description = "" On Error Resume Next Call Demo4_Sub4 Debug.Print "Err in sub4:" & Err.Description ' Err description <> "" Exit Sub err_hander1: Debug.Print "err_hander1 incurred" ' not incurred Resume Next err_hander2: Debug.Print "err_hander2 incurred" ' not incurred Resume Next err_hander3: Debug.Print "err_hander3 incurred" ' incurred Resume Next err_hander4: Debug.Print "err_hander4 incurred" ' incurred Resume Next End Sub Sub Demo4_Sub1() On Error GoTo err_hander Dim i As Long i = 1 / 0 Exit Sub err_hander: Resume Next End Sub Sub Demo4_Sub2() On Error Resume Next Dim i As Long i = 1 / 0 End Sub Sub Demo4_Sub3() On Error GoTo err_hander Dim i As Long i = 1 / 0 Exit Sub err_hander: i = 1 / 0 Resume Next End Sub Sub Demo4_Sub4() On Error GoTo err_hander Dim i As Long i = 1 / 0 Exit Sub err_hander: i = 1 / 0 Resume Next End Sub 6.其它一些注意点6.1.警惕
|