ERROR Handling craps out after 1 loop?? 
Author Message
 ERROR Handling craps out after 1 loop??

Folks,

Could anyone shed some light on this bizzare phenomena?

I have procedure X calling procedure A in a loop.
Procedure A calls procedure B.
Procedure B throws an error using Err.Raise.
Procedure X has the error handling routine and it should grab the error. It
does the first time, but refuses to do it any more. What am I missing
here???

Attached is the simple code:
-----------------------------------------------
Private Sub Command1_Click()
    For i = 1 To 10
        On Error GoTo this1
        f1
        GoTo skip1
    this1:
        MsgBox "this1 hit!"  'This only gets visited once! Why?
    skip1:
        On Error GoTo 0
    Next i
End Sub
-----------------------------------------------
Private Function f1()
    f2
End Function

-----------------------------------------------
Private Function f2() As String
    f2 = "haha"
    Err.Clear
    Err.Raise vbObjectError + 1010, "In f2", "f2 calls a foul ball"
End Function

-----------------------------------------------



Sun, 27 Jun 2004 11:46:30 GMT  
 ERROR Handling craps out after 1 loop??
On Wed, 09 Jan 2002 03:46:30 GMT, "spare_brain"

Oh, this should get Joe going. :-)

Quote:
>Private Sub Command1_Click()
>    For i = 1 To 10
>        On Error GoTo this1
>        f1
>        GoTo skip1
>    this1:
>        MsgBox "this1 hit!"  'This only gets visited once! Why?
>    skip1:

        On Error goto -1 ' instead of goto 0

Quote:
>    Next i
>End Sub

Joe, can you explain the whats and whys of that, coz i can't remember
the reasons.

<snip>

Regards, Frank



Sun, 27 Jun 2004 12:03:18 GMT  
 ERROR Handling craps out after 1 loop??

Quote:
> Oh, this should get Joe going. :-)
 <snip>
> >        MsgBox "this1 hit!"  'This only gets visited once! Why?
> >    skip1:
> On Error goto -1 ' instead of goto 0
> >    Next i

> Joe, can you explain the whats and whys of that, coz i can't remember
> the reasons.
 <snip>
> Regards, Frank

Frank,

Thanks for your response. Can't wait for Joe to take a peek at this. Just
FYI, taking the statement "On Error GoTo 0" out completely does not alter
this bizzarre behavior!

Any suggestions from the experts out there please!!

essbee



Sun, 27 Jun 2004 12:42:38 GMT  
 ERROR Handling craps out after 1 loop??

Quote:
> Folks,

> Could anyone shed some light on this bizzare phenomena?

> I have procedure X calling procedure A in a loop.
> Procedure A calls procedure B.
> Procedure B throws an error using Err.Raise.
> Procedure X has the error handling routine and it should grab the error.
It
> does the first time, but refuses to do it any more. What am I missing
> here???

> Attached is the simple code:
> -----------------------------------------------
> Private Sub Command1_Click()
>     For i = 1 To 10
>         On Error GoTo this1
>         f1
>         GoTo skip1
>     this1:
>         MsgBox "this1 hit!"  'This only gets visited once! Why?
>     skip1:
>         On Error GoTo 0
>     Next i
> End Sub
> -----------------------------------------------
> Private Function f1()
>     f2
> End Function

> -----------------------------------------------
> Private Function f2() As String
>     f2 = "haha"
>     Err.Clear
>     Err.Raise vbObjectError + 1010, "In f2", "f2 calls a foul ball"
> End Function

> -----------------------------------------------

After you handle the error you need to resume, otherwise Basic assumes the
second occurance of an error is an error in the error handler itself.

From VB 4 online help (I don't have VB 6 on this machine) under On Error ...

If an error occurs while an error handler is active (between the occurrence
of the error and a Resume, Exit Sub, Exit Function, or Exit Property
statement), the current procedure's error handler can't handle the error.

I hope this helps.

Best,
Bill
--

--------------------------------------------------------
Due to excessive spam and being tired of redoing my mail
filters every week I now post to newsgroups with a bogus
username in my e-mail address.  To reach me make it



Sun, 27 Jun 2004 13:23:43 GMT  
 ERROR Handling craps out after 1 loop??
On Wed, 09 Jan 2002 04:42:38 GMT, "spare_brain"

Quote:

>Thanks for your response. Can't wait for Joe to take a peek at this. Just
>FYI, taking the statement "On Error GoTo 0" out completely does not alter
>this bizzarre behavior!

Did you notice my little change in skip1 ? (on error goto -1)
That one change made your posted code work as expected.
As opposed to 0,  -1 resets the error trap while *inside* an error
trap, 0 only resets the error, but as i said i can't remember the full
story. In case you've missed it, here is the repost of it working..

Option Explicit

Private Sub Command1_Click()
    Dim i As Integer

    For i = 1 To 10
        On Error GoTo this1
        f1
        GoTo skip1
this1:
        MsgBox "this1 hit!"  'This only gets visited once! Why?
skip1:
        On Error GoTo -1
    Next i
End Sub
Private Function f1()
    f2
End Function

Private Function f2() As String
    f2 = "haha"
    Err.Clear
    Err.Raise vbObjectError + 1010, "In f2", "f2 calls a foul ball"
End Function

Regards, Frank



Sun, 27 Jun 2004 13:42:07 GMT  
 ERROR Handling craps out after 1 loop??

Private Sub Command1_Click()

   On Error GoTo this1

    For i = 1 To 10
        f1
    Next i

On Error Goto 0
Exit Sub

this1:
   MsgBox "this1 hit!"  'This only gets visited once! Why?
   Resume Next

End Sub

--

Happy New Year !

Randy Birch
MVP Visual Basic

http://www.mvps.org/vbnet/

Please respond only to the newsgroups so all can benefit.


Quote:
> Folks,

> Could anyone shed some light on this bizzare phenomena?

> I have procedure X calling procedure A in a loop.
> Procedure A calls procedure B.
> Procedure B throws an error using Err.Raise.
> Procedure X has the error handling routine and it should grab the error.
It
> does the first time, but refuses to do it any more. What am I missing
> here???

> Attached is the simple code:
> -----------------------------------------------
> Private Sub Command1_Click()
>     For i = 1 To 10
>         On Error GoTo this1
>         f1
>         GoTo skip1
>     this1:
>         MsgBox "this1 hit!"  'This only gets visited once! Why?
>     skip1:
>         On Error GoTo 0
>     Next i
> End Sub
> -----------------------------------------------
> Private Function f1()
>     f2
> End Function

> -----------------------------------------------
> Private Function f2() As String
>     f2 = "haha"
>     Err.Clear
>     Err.Raise vbObjectError + 1010, "In f2", "f2 calls a foul ball"
> End Function

> -----------------------------------------------



Sun, 27 Jun 2004 14:03:31 GMT  
 ERROR Handling craps out after 1 loop??
A little hint before somebody cremates you. Check out the Resume Next
statement.

Jason Bouzane


Quote:


> > Oh, this should get Joe going. :-)
>  <snip>
> > >        MsgBox "this1 hit!"  'This only gets visited once! Why?
> > >    skip1:
> > On Error goto -1 ' instead of goto 0
> > >    Next i

> > Joe, can you explain the whats and whys of that, coz i can't remember
> > the reasons.
>  <snip>
> > Regards, Frank

> Frank,

> Thanks for your response. Can't wait for Joe to take a peek at this. Just
> FYI, taking the statement "On Error GoTo 0" out completely does not alter
> this bizzarre behavior!

> Any suggestions from the experts out there please!!

> essbee



Sun, 27 Jun 2004 14:01:41 GMT  
 ERROR Handling craps out after 1 loop??

Quote:

> Folks,

> Could anyone shed some light on this bizzare phenomena?

> I have procedure X calling procedure A in a loop.
> Procedure A calls procedure B.
> Procedure B throws an error using Err.Raise.
> Procedure X has the error handling routine and it should grab the error. It
> does the first time, but refuses to do it any more. What am I missing
> here???

> Attached is the simple code:
> -----------------------------------------------
> Private Sub Command1_Click()
>     For i = 1 To 10
>         On Error GoTo this1
>         f1

Who needs the function calls?  Just use: MsgBox i / 0

Quote:
>         GoTo skip1
>     this1:
>         MsgBox "this1 hit!"  'This only gets visited once! Why?

You never leave the "error handler" mode.  Until you do a Resume [whatever],
an Exit Sub, or an *undocumented* On Error GoTo -1, you're still within the
error handler.  Maybe if you put a Resume skip1 here:

          Resume skip1

Quote:
>     skip1:
>         On Error GoTo 0
>     Next i
> End Sub

There's a good reason most developers put the error handlers at the end of
the function or subroutine -- it's much easier to remember that you're in
"sudden death" while the error handler is running, and it's much harder to
jump back into the "real" code without first leaving "sudden death" mode.

--
Joe Foster <mailto:jlfoster%40znet.com>  Sign the Check! <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!



Sun, 27 Jun 2004 14:48:09 GMT  
 ERROR Handling craps out after 1 loop??

Quote:
> Folks,

<snip>

Thanks guys!! Both your suggestion worked like charm! To summarize:
You have to let the compiler know that the error HAS been handled. Do this
by either of the following:
1. Insert a "Resume Next"
2. Insert an undocumented "On Error GoTo -1"

after the handled code.

essbee



Sun, 27 Jun 2004 20:54:33 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. ERROR Handling craps out after 1 loop??

2. How to handle print outs in VB?

3. Error handling inside of a loop

4. Can't Unload in this context Error (This is crap)

5. Error Handling (Handle by number)

6. Error Handling Is not Handling

7. university EMR send outs

8. on the outs

9. Learning the ins and outs of Peek and Poke

10. Filesystem Time outs

11. Session Variable Time-outs

12. Time-outs

 

 
Powered by phpBB® Forum Software