Debugging Error Handlers

Why does the ON ERROR fail the second time through? It works right the first time but crashes after the error is encountered after passing through errorhandler:

Option Explicit
Dim x As Integer

Private Sub Form_Load()
    On Error GoTo errorhandler
restart:
    x = 0
    Do
        x = x + 1
        If x = 2 Then Err.Raise 51
    Loop While x < 10
    Exit Sub
errorhandler:
GoTo restart
End Sub
Bandit88_JohnsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Zamba1Commented:
In your error handler try "Resume restart" instead of "GoTo restart".

As you do a "GoTo", VB treats everything as part of the error handler. With "Resume" you explicitely end the error handler and resume execution at the specified label.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nffvrxqgrcfqvvcCommented:
'count how many times our error gets hit
Static ErrorCount As Integer
Dim x As Integer
    On Error GoTo errorhandler
restart:
    x = 0
    Do
        x = x + 1
        If x = 2 Then x = x \ 0 'create a division by zero error.
    Loop While x < 10
    Exit Sub
errorhandler:
ErrorCount = ErrorCount + 1
Debug.Print "Error hits: " & " " & ErrorCount
Debug.Print err.Number & " " & err.Description
Resume Next
edwardiiiCommented:
Hi, Bandit88_Johnson. Is there any difference in behavior if you move the "On Error" line under the "restart:" line item? It may be that the use of "GoTo restart" versus "Resume Next" is causing the code to ignore the intial "On Error..." statement, and the forcing of an "Internal Error" may be causing the lockup.  This article elaborates on Err.Raise (though it relates directly to VB 4, it may be helpful):

     http://support.microsoft.com/default.aspx?scid=kb;en-us;129939
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.