Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Resume and Goto in error handling block

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?

like image 504
Richard Oliver Avatar asked Jun 08 '10 11:06

Richard Oliver


People also ask

What are the 3 different types of error-handling techniques in VBA?

AutoCAD to Excel - VBA Programming Hands-On! There are three types of errors in programming: (a) Syntax Errors, (b) Runtime Errors, and (c) Logical Errors.

What does On error GoTo do?

On Error GoTo lineEnables the error-handling routine that starts at line specified in the required line argument. The line argument is any line label or line number. If a run-time error occurs, control branches to line, making the error handler active.

How do you resume after an error in VBA code?

Resume. The Resume statement tells VBA to resume executing code at a specified point. Resume can only be used in an error handling routine, any other use will generate an error. Using just Resume causes execution to resume at the same line of code that caused the error.

How Do I Stop On error Resume Next?

Basic Error Handling Overview To shut off (disable) the active handler, use On Error GoTo 0 . Doing so will close off the code block that uses that handler. Alternatively, exit the subroutine using Exit Sub , which automatically turns off the handler.


1 Answers

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.

like image 123
MarkJ Avatar answered Sep 28 '22 02:09

MarkJ