Home > Archive > Visual Basic Syntax > April 2006 > On Error GoTo does not work
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
On Error GoTo does not work
|
|
|
| Hello,
I have an odd problem, where in vb6 app in a method
we do On Error GoTo blob: and make a call to a WebService
to connect to it. when we do this, if the service is not reachable we need
to handle the error. but we get an unspecified exception and the error block
we specified does not get executed.
how do we catch this exception?
any help is appreciated thanks,
| |
| Tony Proctor 2006-04-04, 8:03 am |
| Can you provide a code sample for this so that we can understand what you're
doing more clearly?
Any code at an error label is deemed to be in an "active error handler" for
that procedure level. Hence, if that code incurs a further exception then it
cannot be handled at that same current procedure level. It would be caught
by an error handler in a previous procedure level - if any - or it would be
fatal.
In order to set a new error trap at the same procedure level then you must
first execute a Resume statement, or an 'On Error Goto -1'. Then another 'On
Error Goto Label' will work.
Tony Proctor
"mrb12" <mrb12@discussions.microsoft.com> wrote in message
news:F7708929-E6E3-46F7-9A14-8EE3BD5AF7C7@microsoft.com...
> Hello,
> I have an odd problem, where in vb6 app in a method
> we do On Error GoTo blob: and make a call to a WebService
> to connect to it. when we do this, if the service is not reachable we need
> to handle the error. but we get an unspecified exception and the error
block
> we specified does not get executed.
>
> how do we catch this exception?
> any help is appreciated thanks,
| |
|
| thanks Tony. that was the case; the layer was 2nd layer that i was handling
the error; however the 4th layer down was throwing an exception which was
unhandled and fatal. so i had to go into a 3rd party's code and add exception
handling.
regards.
"Tony Proctor" wrote:
> Can you provide a code sample for this so that we can understand what you're
> doing more clearly?
>
> Any code at an error label is deemed to be in an "active error handler" for
> that procedure level. Hence, if that code incurs a further exception then it
> cannot be handled at that same current procedure level. It would be caught
> by an error handler in a previous procedure level - if any - or it would be
> fatal.
>
> In order to set a new error trap at the same procedure level then you must
> first execute a Resume statement, or an 'On Error Goto -1'. Then another 'On
> Error Goto Label' will work.
>
> Tony Proctor
>
> "mrb12" <mrb12@discussions.microsoft.com> wrote in message
> news:F7708929-E6E3-46F7-9A14-8EE3BD5AF7C7@microsoft.com...
> block
>
>
>
| |
| Tony Proctor 2006-04-04, 8:03 am |
| Maybe this illustrates what I meant better:
Private Sub Form_Load()
On Error GoTo ErrLab1
Err.Raise vbObjectError + 513, TypeName(Me), "First error"
Exit Sub
ErrLab1:
MsgBox Err.Description
Resume Lab1 '<<<<<
Lab1: '<<<<<
On Error GoTo ErrLab2
Err.Raise vbObjectError + 514, TypeName(Me), "Second error"
Exit Sub
ErrLab2:
MsgBox Err.Description
End Sub
Without the highlighted lines, the second error would be fatal
Tony Proctor
"mrb12" <mrb12@discussions.microsoft.com> wrote in message
news:EE344000-B76C-44C0-BD60-278DEDAD4D1F@microsoft.com...
> thanks Tony. that was the case; the layer was 2nd layer that i was
handling
> the error; however the 4th layer down was throwing an exception which was
> unhandled and fatal. so i had to go into a 3rd party's code and add
exception[color=darkred]
> handling.
> regards.
>
> "Tony Proctor" wrote:
>
you're[color=darkred]
for[color=darkred]
then it[color=darkred]
caught[color=darkred]
be[color=darkred]
must[color=darkred]
'On[color=darkred]
need[color=darkred]
| |
|
| Tony Proctor wrote:
> Maybe this illustrates what I meant better:
>
> Private Sub Form_Load()
> On Error GoTo ErrLab1
>
> Err.Raise vbObjectError + 513, TypeName(Me), "First error"
> Exit Sub
>
> ErrLab1:
> MsgBox Err.Description
> Resume Lab1 '<<<<<
> Lab1: '<<<<<
> On Error GoTo ErrLab2
> Err.Raise vbObjectError + 514, TypeName(Me), "Second error"
> Exit Sub
>
> ErrLab2:
> MsgBox Err.Description
> End Sub
>
> Without the highlighted lines, the second error would be fatal
>
> Tony Proctor
>
Where did you find this, or have you learned it by experience?
This is completely new for me! I thought that executing an 'On Error
GoTo' cleared the Error Object so the Resume you write shouldn't be
required.
Sinna
| |
| Tony Proctor 2006-04-05, 8:04 am |
| You're talking about something else there. It's true that 'On Error' (and
Err.Clear) clears down the Err object. However, what I'm talking about here
is whether the VB run-time thinks you're currently in an error handler or
not. Once you've trapped to an error label then you're deemed to be in an
error handler for the current procedure level, and any further exceptions
cannot be handled at that same level. This situation continues until you
execute a 'Resume' statement, or the special 'On Error Goto -1', or you
return from the current procedure.
One of the most common error-handling bugs I've seen is when someone writes
an 'On Error Goto' statement at the head of a loop that directs control to a
label at the bottom of the loop. For instance, someone is processing records
from a file or table, and they want any processing errors to merely skip the
current record. For example:
On Error Goto LabContinue
Do While somecondition
...process a record...
LabContinue:
Loop
What happens here is that the first processing error is trapped OK, but the
second is either handled by an outer-level procedure, or is fatal if no
outer-level procedure has an error handler. The reason being that the rest
of the iterations are then executing in the context of the error handler. An
easy way to avoid this is something like:
On Error Goto ErrHandler
Do While somecondition
...process a record...
LabContinue:
Loop
Exit Sub
ErrHandler:
Resume LabContinue
Tony Proctor
"Sinna" <news4sinna_NOSPAM@hotpop.com> wrote in message
news:uC2JThHWGHA.3740@TK2MSFTNGP03.phx.gbl...
> Tony Proctor wrote:
> Where did you find this, or have you learned it by experience?
> This is completely new for me! I thought that executing an 'On Error
> GoTo' cleared the Error Object so the Resume you write shouldn't be
> required.
>
> Sinna
|
|
|
|
|