Thanks Thanks:  0
Likes Likes:  0
Results 1 to 5 of 5

Thread: Error handling Resume v Goto

  1. #1
    Board Regular
    Join Date
    Apr 2013
    Posts
    812
    Post Thanks / Like
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Default Error handling Resume v Goto

    According to this article by Chip Pearson, you should not use GoTo in error handling.

    Error Handling In VBA

    For example:

    Code:
    Dim MyArray(1 To 10) As VariantDim Counter As Integer
    On Error GoTo Errhandler
    For Counter = 1 To 20
        MyArray(Counter) = Counter
    Next Counter
    Cont:
        MsgBox "Finished"
    Exit Sub
    Errhandler:
        MsgBox "Error"
        
        GoTo Cont ' SHOULD USE RESUME Cont

    I do agree with him as I have come across an example where using GoTo did not achieve the desired result (although the example above does work with GoTo).

    I could have added an error reset like this (and in examples that didn't work without it, it still didn't work with it):

    Code:
    Dim MyArray(1 To 10) As Variant Dim Counter As Integer
    On Error GoTo Errhandler
    For Counter = 1 To 20
        MyArray(Counter) = Counter
    Next Counter
    Cont:
        MsgBox "Finished"
    Exit Sub
    Errhandler:
        MsgBox "Error"
        
        On Error Goto 0 ' Error reset
    
        GoTo Cont ' SHOULD USE RESUME Cont

    What Chip Pearson did not say is what actually happens if you did use Goto instead of Resume.

    Does anybody know?

    Thanks

  2. #2
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    UK
    Posts
    32,351
    Post Thanks / Like
    Mentioned
    25 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Error handling Resume v Goto

    If you don't use Resume, you don't clear the exception that was raised when the error occurs and any further errors will not be handled. On Error Goto 0 does not do this either. See my blog here: On Error WTF? | Excel Matters

  3. #3
    Board Regular
    Join Date
    Apr 2013
    Posts
    812
    Post Thanks / Like
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Error handling Resume v Goto

    Quote Originally Posted by RoryA View Post
    If you don't use Resume, you don't clear the exception that was raised when the error occurs and any further errors will not be handled. On Error Goto 0 does not do this either. See my blog here: On Error WTF? | Excel Matters
    Thanks for your artcile but I don't think I fully understand one point.

    Code:
    The only ways to reset an active error condition and deactivate an error handler are via a Resume, Exit Sub, Exit Function, or Exit Property statement, or via an On Error Goto -1 statement.
    Further on, you say:

    Code:
    If you find yourself using On Error Goto -1 a lot (or at all), you need to stop and rethink what you are doing! I have never, ever, seen well-written code that required it and have never used it myself in actual production code.
    Doesn't your first quote state using On Error GoTo -1 is a valid way of resetting the error?

    I added the line:

    Code:
    On Error GoTo -1
    to an example of mine (not the one shown above) that originally failed and it now works.
    Last edited by tiredofit; Oct 22nd, 2015 at 05:52 AM.

  4. #4
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    UK
    Posts
    32,351
    Post Thanks / Like
    Mentioned
    25 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Error handling Resume v Goto

    Yes it does say that it's a valid way. Just as using Goto or GoSub are valid ways of doing things; or simply putting On Error Resume Next at the top of your code. That does not mean that they should be your first thought. There are generally better ways of achieving the same ends (which is one reason I suspect you don't see On Error Goto -1 either documented or used very often).

  5. #5
    Board Regular
    Join Date
    Apr 2013
    Posts
    812
    Post Thanks / Like
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Error handling Resume v Goto

    Thanks

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •