none
Difference between resume and GoTo in Error handling? RRS feed

  • Question

  • If ans=vbYes then GoTo TryAgain

    If ans=vbYes then Resume TryAgain

    In this 2 codes, I couldn't find any difference while running. Can you please tell me..

    1. What is the use of resume over Goto?

    2. What is meaning of clearing error condition? How it will be useful?

    Saturday, May 12, 2012 6:51 AM

Answers

  • You can use GoTo anywhere in a procedure/function to continue code execution at another place within the same procedure/function. You should use it sparingly, for excessive use of GoTo leads to "spaghetti code" that is difficult to follow.

    Resume can only be used in the context of error handling. There are four forms:

    On Error Resume Next

    If an error occurs below this line, the user will not get an error message. Code execution will continue on the line after the line that caused the error. The Err object will contain information about the error that occurred; you can inspect if desired.

    The following three forms can only be used within an error handling section. Use of any of these forms will clear the Err object, i.e. code continues as if there hasn't been an error.

    Resume

    The code will continue execution on the line that caused the error. This will only work if you removed the cause of the error in the error handling section.

    Resume Next

    The code will continue execution on the line below the one that caused the error. You must have ensured that this will not lead to a new error.

    Resume MyLabel

    The code will continue execution at the label MyLabel in the code. This is similar to GoTo MyLabel, but as mentioned above, you can only do this from an error handling section, and use of Resume will clear the Err object.

    Regards, Hans Vogelaar

    • Marked as answer by Vbaer Saturday, May 12, 2012 12:06 PM
    Saturday, May 12, 2012 11:23 AM