views:

87

answers:

1

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?

+6  A: 

Both transfer execution to the Cleanup label. As far as I can remember, the only differences are

  • Using Goto doesn't clear the Err object (so Err.Clear is necessary if you use Goto) and it leaves your error handler disabled. If an error occurs after the Cleanup label, it won't be handled at ErrorHandler.
  • Using 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 the Cleanup lable, it will be handled at ErroHandler

The VB6 manual entry for the Resume statement doesn't explain these differences.

MarkJ
Cheers MarkJ, learn't alot from this answer.
Rich Oliver
While testing i've found that if an error is raised in CleanUp an infinte loop will exist bouncing between the ErrorHandler and CleanUp blocks.Is the recommended technique therefore to have On Error Resume Next in the CleanUp?
Rich Oliver
MarkJ