Error handling Resume v Goto

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,080
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
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,178
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
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
 

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,080
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.

Rich (BB 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:

Rich (BB 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:

Rich (BB code):
On Error GoTo -1
to an example of mine (not the one shown above) that originally failed and it now works.
 
Last edited:

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,178
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
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).
 

Forum statistics

Threads
1,078,253
Messages
5,339,103
Members
399,278
Latest member
randomNumberGenerator2211

Some videos you may like

This Week's Hot Topics

Top