I understand that in the following example a Resume statement should be used instead of a Goto statement.
Sub Method()
On Error Goto ErrorHandler
...
CleanUp:
...
Exit Function
ErrorHandler:
Log error etc
Err.Clear 'Is this line actually necessary?'
Resume CleanUp 'SHOULD USE THIS'
Goto CleanUp 'SHOULD NOT USE THIS'
End Sub
My question is what difference is there in the execution of the two?
This is BIG misunderstanding! There is important DIFFERENCE between:
and:
NEVER use the first form, ALWAYS use Resume CleanUp (only). Resume do the RESET of internal VB6 error state, so when OTHER error is occured, "On Error GoTo Label" will be applied. I you use "Err.Clear" then Err object is cleared, but INTERNAL error state is NOT cleared and when another error occure, it is considered as code WITHOUT any exception handler and throws outside of the function. You CAN NOT fix it by using "On Error GoTo Label2"
Consider this code:
If you run this, it will interrupt on "ERROR!" line. If you replace "Err.Clear" with "Resume L0" then execution does not interrupts on "ERROR!" line and code jumps to "L2" label and provides "MsgBox Err"
Both transfer execution to the
Cleanup
label. As far as I can remember, the only differences areGoto
doesn't clear the Err object (soErr.Clear
is necessary if you useGoto
) and it leaves your error handler disabled. If an error occurs after theCleanup
label, it won't be handled atErrorHandler
.Resume
clears the Err object and it switches your error handler back on (it is disabled while it is handling errors). If an error occurs after theCleanup
lable, it will be handled atErroHandler
The VB6 manual entry for the Resume statement doesn't explain these differences.