我在主调用过程上使用错误处理程序,并让其他过程汇总到该错误处理程序。
我应该每次都清除错误吗?或者我应该 Exit Sub 而不是让错误处理程序在 End Sub 上继续?
我问这个问题是因为我读到我可能会捕获第一个错误,然后其他错误将不会被处理。
很抱歉,如果这不太清楚。不太确定我在说什么。
谢谢!!
编辑:类似这样的事情。有这个必要吗?
Public Sub SubA()
On Error Goto ProcError
' other code
MsgBox FuncA()
ProcExit:
Exit Sub
ProcError:
MsgBox Err.Description
Resume ProcExit
End Sub
最佳答案
OP:我应该每次都清除错误吗?或者我应该 Exit Sub 而不是让错误处理程序在 End Sub 上继续?
清除错误是什么意思?
通常程序是这样写的
public sub myProcedure()
on error goto e:
'statements that could raise error
exit sub
e:
Log err.Number, err.Description, "error occurred in myProcedure"
end sub
或者
您可以选择不添加错误处理程序。在这种情况下,错误将传播到调用者(或处理错误的过程)。
请发布您想要实现的目标和期望的示例代码。
编辑:这是您发布的代码的含义
Public Sub SubA()
On Error Goto ProcError
' other code
MsgBox FuncA()
exit sub 'put exit sub otherwise it will execute the line below, even if no error
ProcExit:
'this statement will get executed after the error msgbox is shown
msgbox "Reached ProcExit"
Exit Sub
ProcError:
MsgBox Err.Description 'catch the error and show the msgbox
'error is cleared the point it reaches ProcError
'Resume ProcExit acts as goto, to execute any statements after the error is handled
Resume ProcExit
End Sub
关于VB6错误传播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1376964/