Results 1 to 10 of 10
  1. #1
    Star Lounger
    Join Date
    Apr 2002
    Location
    Nottingham, Nottinghamshire, England
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Word Error Handling (Word 97 SR2)

    Could someone please explain the following line

    On Error GoTo -1: On Error GoTo HandleError

    I understand "On Error GoTo 0" and "On Error GoTo HandleError", but I don't know the meaning of "On Error GoTo -1" or the purpose of the colon.

    TIA

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word Error Handling (Word 97 SR2)

    I'm not sure about the Goto -1; I would guess that it has the same effect as Goto 0.
    The colon is used to place multiple instructions on one line. In the Stone Age of Basic programming, when memory and screen real estate were very limited, this was a very useful feature. Nowadays, it's not really necessary any more.

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Error Handling (Word 97 SR2)

    The goto -1 resets the error handling so that you can trap exceptions in your error handlers and so you can nest error handlers.
    As far as I know, it's supported from VB5 thru VB.Net
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Word Error Handling (Word 97 SR2)

    That one's not in my "...in a Nutshell" book. Here are some references for future, uh, reference:

    Visual Basic Language Reference - On Error Statement
    Visual Basic Language Specification - 8.11.2.2 On Error Statement

    These sources suggest that using On Error GoTo -1 clears the outstanding error condition, which is a bit more than clearing the error handler, but as a practical matter, a seemingly necessary action before changing it. I'm struggling to think of a situation in which I would use it, since a Resume Next statement at the end of an error handler has the same effect in my test:

    Sub ErrorTester()
    Dim intCount As Integer
    intCount = 1
    On Error GoTo errHandler1
    MsgBox intCount / 0
    intCount = intCount + 1
    MsgBox intCount / 0
    On Error GoTo errHandler2
    intCount = intCount + 1
    MsgBox intCount / 0
    On Error GoTo -1
    intCount = intCount + 1
    MsgBox intCount / 0
    On Error GoTo errHandler3
    intCount = intCount + 1
    MsgBox intCount / 0
    LastLabel:
    intCount = intCount + 1
    ' Because we got here without a Resume, the next division
    ' error is not handled unless previous error is canceled
    ' Err.Clear 'does not reset error condition/handler
    ' On Error GoTo -1 'causes infinite loop, due to hardcoded GoTo
    Stop
    MsgBox intCount / 0
    Exit Sub

    errHandler1:
    Debug.Print "Test " & intCount & ": invoked errHandler1"
    Resume Next
    errHandler2:
    Debug.Print "Test " & intCount & ": invoked errHandler2"
    Resume Next
    errHandler3:
    Debug.Print "Test " & intCount & ": invoked errHandler3"
    ' This next line does not reset error handling...
    GoTo LastLabel
    End Sub

    Any thoughts on why we would ever need it?

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Error Handling (Word 97 SR2)

    Kevin's explanation is kerect.

    I've never seen the use of -1 documented by MSFT, but they use it in their own published code in the KB and maybe elsewhere.

  6. #6
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Error Handling (Word 97 SR2)

    Take a look at MSFT KB articles 177271 and 202019.

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Word Error Handling (Word 97 SR2)

    In those examples, MS has:

    <pre>Sub Main()
    ' Loop 10 times.
    For i = 1 To 10
    ' Clear error trap. Set error trap.
    On Error GoTo -1: On Error GoTo ErrorTrap
    ' Artificially raise an error.
    Err.Raise 5
    ErrorTrap:
    ' Decision tree based on error returned.
    If Err.Number = 0 Then
    ' If no error occurred, do this...
    count_ = count_ + 1
    Else
    ' Else if error occurred, do this...
    Err.Number = 0
    End If
    Next
    End Sub</pre>


    In that context, one could instead use On Error Resume Next and handle the error inline:

    <pre>Sub Main()
    ' Loop 10 times.
    For i = 1 To 10
    On Error Resume Next
    ' Artificially raise an error.
    Err.Raise 5
    ' Decision tree based on error returned.
    If Err.Number = 0 Then
    ' If no error occurred, do this...
    count_ = count_ + 1
    Else
    ' Else if error occurred, do this...
    Err.Number = 0
    End If
    Next
    End Sub</pre>


    Or one could handle the error outside the loop with a Resume Next to reset the Error object:

    <pre>Sub Main()
    ' Loop 10 times.
    For i = 1 To 10
    On Error GoTo ErrorTrap
    ' Artificially raise an error.
    Err.Raise 5
    Next
    Exit Sub

    ErrorTrap:
    ' Decision tree based on error returned.
    If Err.Number = 0 Then
    ' If no error occurred, do this...
    count_ = count_ + 1
    Else
    ' Else if error occurred, do this...
    Err.Number = 0
    End If
    Resume Next
    End Sub</pre>


    I don't think these articles demonstrate a necessity for On Error GoTo -1

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Error Handling (Word 97 SR2)

    Some time ago, I constructed such an example.
    Dunno where I saved or posted the critter.

  9. #9
    Star Lounger
    Join Date
    Apr 2002
    Location
    Nottingham, Nottinghamshire, England
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Error Handling (Word 97 SR2)

    Wow, thanks for all the input guys. At least I understand what's happening now:-)

  10. #10
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Error Handling (Word 97 SR2)

    There was a branch of this thread, to do with .NET error handling, which was moved to the .NET board
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

Posting Permissions

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