This is a multi-part message in MIME format.

------=_NextPart_000_0026_01C7170E.4D45F5C0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Can someone help me understand why TestA works and TestB and TestC do =
not:

Sub TestA()
On Error Resume Next
Err.Raise 6
On Error GoTo SubErr:
Err.Raise 13
SubErr:
MsgBox "Eureka!!"
End Sub

Sub TestB()
On Error GoTo Err_Handler
Err.Raise 6
Err_Handler:
On Error GoTo SubErr:
Err.Raise 13
SubErr:
MsgBox "Eureka!!"
End Sub

Sub TestC()
On Error GoTo Err_Handler
Err.Raise 6
Err_Handler:
On Error Resume Next
Err.Raise 13
MsgBox "Eureka!!"
End Sub

In TestB and TestC, I have tried about every combination of GoTo, =
Resume, Err.Clear, that I can think of. I don't understand why the code =
isn't handling the second error. thanks.
--=20
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

------=_NextPart_000_0026_01C7170E.4D45F5C0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.5730.11" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#f8f2d6>
<DIV>Can someone help me understand why TestA works and TestB and TestC =
do=20
not:</DIV>
<DIV>&nbsp;</DIV>
<DIV>Sub TestA()<BR>On Error Resume Next<BR>Err.Raise 6<BR>On Error GoTo =

SubErr:<BR>Err.Raise 13<BR>SubErr:<BR>MsgBox "Eureka!!"<BR>End =
Sub<BR></DIV>
<DIV>Sub TestB()<BR>On Error GoTo Err_Handler<BR>Err.Raise=20
6<BR>Err_Handler:<BR>On Error GoTo SubErr:<BR>Err.Raise =
13<BR>SubErr:<BR>MsgBox=20
"Eureka!!"<BR>End Sub<BR></DIV>
<DIV>Sub TestC()<BR>On Error GoTo Err_Handler<BR>Err.Raise=20
6<BR>Err_Handler:<BR>On Error Resume Next<BR>Err.Raise 13<BR>MsgBox=20
"Eureka!!"<BR>End Sub<BR></DIV>
<DIV>In TestB and TestC, I have tried about every combination of GoTo, =
Resume,=20
Err.Clear, that I can think of.&nbsp; I don't understand why the code =
isn't=20
handling the second error.&nbsp; thanks.<BR>-- <BR>Greg Maxey/Word=20
MVP<BR>See:<BR><A=20
href=3D"http://gregmaxey.mvps.org/word_tips.htm">http://gregmaxey.mvps.or=
g/word_tips.htm</A><BR>For=20
some helpful tips using Word.<BR></DIV></BODY></HTML>

------=_NextPart_000_0026_01C7170E.4D45F5C0--

Re: I thought that I had a basic understanding of errors by Jezebel

Jezebel
Sun Dec 03 19:20:41 CST 2006

This is a multi-part message in MIME format.

------=_NextPart_000_000A_01C7179E.9D5BA8D0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Not sure quite what you mean by 'working' in respect of TestA -- it =
ignores the first error, and jumps to SubErr for the second.

But the big issue, is that none of your error-handlers actually =
'handles' the error. An error is handled when the function exits or you =
execute a resume or resume [label] instruction. Until you do that, the =
code is still running within the context of the original-handler, and =
any further errors are passed 'up the line' to an error-handler in the =
calling function, if there is one. So a) the error-handler can't deal =
with errors within the error-handler itself, and b) you can't issue a =
new 'on error goto ...' within the context of the error-handling code. =
Try this --

Sub TestA()
On Error GoTo SubErr:
TestB

SubErr:
MsgBox "Error in Test A"
End Sub

Sub TestB()
On Error GoTo Err_Handler
Err.Raise 6

Err_Handler:
On Error GoTo Err_Handler:
MsgBox "Error in TestB"
Err.Raise 13

End Sub


The moral is, don't try to do clever things in the error-handling code: =
just handle the error and resume, either back to the code or to the exit =
point.




"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message =
news:%23YeW2gzFHHA.960@TK2MSFTNGP04.phx.gbl...
Can someone help me understand why TestA works and TestB and TestC do =
not:

Sub TestA()
On Error Resume Next
Err.Raise 6
On Error GoTo SubErr:
Err.Raise 13
SubErr:
MsgBox "Eureka!!"
End Sub

Sub TestB()
On Error GoTo Err_Handler
Err.Raise 6
Err_Handler:
On Error GoTo SubErr:
Err.Raise 13
SubErr:
MsgBox "Eureka!!"
End Sub

Sub TestC()
On Error GoTo Err_Handler
Err.Raise 6
Err_Handler:
On Error Resume Next
Err.Raise 13
MsgBox "Eureka!!"
End Sub

In TestB and TestC, I have tried about every combination of GoTo, =
Resume, Err.Clear, that I can think of. I don't understand why the code =
isn't handling the second error. thanks.
--=20
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

------=_NextPart_000_000A_01C7179E.9D5BA8D0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2900.2995" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#f8f2d6>
<DIV><FONT face=3DArial size=3D2>Not sure quite what you mean by =
'working' in=20
respect of TestA -- it ignores the first error, and jumps to SubErr for =
the=20
second.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>But the big issue, is that none of your =

error-handlers actually 'handles' the error. An error is handled =
when&nbsp;the=20
function exits or you execute a resume or resume [label] instruction. =
Until you=20
do that, the code is still running within the context of the =
original-handler,=20
and any further errors are passed 'up the line' to an error-handler in =
the=20
calling function, if there is one. So a) the error-handler can't deal =
with=20
errors within the error-handler itself, and b) you can't issue a new 'on =
error=20
goto ...' within the context of the error-handling code. Try this=20
--</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Sub TestA()</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>On Error GoTo =
SubErr:<BR>TestB</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>SubErr:<BR>MsgBox "Error in Test =
A"<BR>End=20
Sub</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Sub TestB()<BR>On Error GoTo=20
Err_Handler<BR>Err.Raise 6</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Err_Handler:<BR>On Error GoTo=20
Err_Handler:<BR>MsgBox "Error in TestB"<BR>Err.Raise 13</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>End Sub</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>The moral is, don't try to do clever =
things in the=20
error-handling code: just handle the error and resume, either back to =
the code=20
or to the exit point.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV>"Greg Maxey" &lt;<A=20
=
href=3D"mailto:gmaxey@mvps.oSCARrOMEOgOLF">gmaxey@mvps.oSCARrOMEOgOLF</A>=
&gt;=20
wrote in message <A=20
=
href=3D"news:%23YeW2gzFHHA.960@TK2MSFTNGP04.phx.gbl">news:%23YeW2gzFHHA.9=
60@TK2MSFTNGP04.phx.gbl</A>...</DIV>
<DIV>Can someone help me understand why TestA works and TestB and =
TestC do=20
not:</DIV>
<DIV>&nbsp;</DIV>
<DIV>Sub TestA()<BR>On Error Resume Next<BR>Err.Raise 6<BR>On Error =
GoTo=20
SubErr:<BR>Err.Raise 13<BR>SubErr:<BR>MsgBox "Eureka!!"<BR>End =
Sub<BR></DIV>
<DIV>Sub TestB()<BR>On Error GoTo Err_Handler<BR>Err.Raise=20
6<BR>Err_Handler:<BR>On Error GoTo SubErr:<BR>Err.Raise=20
13<BR>SubErr:<BR>MsgBox "Eureka!!"<BR>End Sub<BR></DIV>
<DIV>Sub TestC()<BR>On Error GoTo Err_Handler<BR>Err.Raise=20
6<BR>Err_Handler:<BR>On Error Resume Next<BR>Err.Raise 13<BR>MsgBox=20
"Eureka!!"<BR>End Sub<BR></DIV>
<DIV>In TestB and TestC, I have tried about every combination of GoTo, =
Resume,=20
Err.Clear, that I can think of.&nbsp; I don't understand why the code =
isn't=20
handling the second error.&nbsp; thanks.<BR>-- <BR>Greg Maxey/Word=20
MVP<BR>See:<BR><A=20
=
href=3D"http://gregmaxey.mvps.org/word_tips.htm">http://gregmaxey.mvps.or=
g/word_tips.htm</A><BR>For=20
some helpful tips using Word.<BR></DIV></BLOCKQUOTE></BODY></HTML>

------=_NextPart_000_000A_01C7179E.9D5BA8D0--


Re: I thought that I had a basic understanding of errors by Jay

Jay
Sun Dec 03 19:23:44 CST 2006

Hi Greg,

I'll quote the Remarks section of the help topic for the On Error
statement:

"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. Control returns to the calling procedure. If the
calling procedure has an enabled error handler, it is activated to
handle the error. If the calling procedure's error handler is also
active, control passes back through previous calling procedures until
an enabled, but inactive, error handler is found. If no inactive,
enabled error handler is found, the error is fatal at the point at
which it actually occurred."

Looking at your examples, TestA only has one handler and it's
triggered by the Err.Raise 13. That is, the Resume Next doesn't count
as an "active error handler". In TestB and TestC, you're trying to
catch an error while you're already inside an active error handler,
and that isn't allowed.

To see what the help is talking about wrt a calling procedure, try
single-stepping with F8 through this code, starting in TestD:

Sub TestD()
On Error GoTo Err_HandlerD
Call TestE
Exit Sub

Err_HandlerD:
MsgBox "D: Eureka!! (" & Err.Number & ")"
End Sub
'~~~~~~~~~~~~
Sub TestE()
On Error GoTo Err_HandlerE
Err.Raise 6
Exit Sub

Err_HandlerE:
On Error GoTo SubErr
Err.Raise 13
Exit Sub

SubErr:
MsgBox "E: Eureka (" & Err.Number & ")!!"
End Sub

At a language-design level, I think this restriction was put into VBA
to avoid having to deal with some nasty issues. You can easily get
into infinite loops when you allow error-handling that itself can
cause errors. OTOH, there are legitimate circumstances where you want
to do something of this sort -- for example, if an error occurs and
you want to log it, but you get an error from the OS when you try to
open the log file. There are ways to handle that with only On Error
Resume Next.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Sun, 3 Dec 2006 19:07:39 -0500, "Greg Maxey"
<gmaxey@mvps.oSCARrOMEOgOLF> wrote:

>Can someone help me understand why TestA works and TestB and TestC do not:
>
>Sub TestA()
>On Error Resume Next
>Err.Raise 6
>On Error GoTo SubErr:
>Err.Raise 13
>SubErr:
>MsgBox "Eureka!!"
>End Sub
>
>Sub TestB()
>On Error GoTo Err_Handler
>Err.Raise 6
>Err_Handler:
>On Error GoTo SubErr:
>Err.Raise 13
>SubErr:
>MsgBox "Eureka!!"
>End Sub
>
>Sub TestC()
>On Error GoTo Err_Handler
>Err.Raise 6
>Err_Handler:
>On Error Resume Next
>Err.Raise 13
>MsgBox "Eureka!!"
>End Sub
>
>In TestB and TestC, I have tried about every combination of GoTo, Resume, Err.Clear, that I can think of. I don't understand why the code isn't handling the second error. thanks.

Jay/Jezebel by Greg

Greg
Mon Dec 04 06:52:35 CST 2006

Thanks Gents,

I did reply last night, but see this morning that it didn't transmit.
I have had a few error messages the past two days using OE that
newsgroups coudn't be resolved?

Anyway, thanks for both replies. Jezebel, by working I just meant the
code would run without generating a runtime error. With your reply and
Jay's I see now why it would and the others wouldn't. It was a poor
example.

The basic problem was this. I wanted an error handler that when an
error was generated that it would simply ignore it an go on to save the
document. I was working with TestA below which worked OK except when
the user canceled or "X" out of the SaveAs Dialog. It would then throw
and error while in the error handler.

Sub TestA()
On Error GoTo Err_Handler
MsgBox ActiveDocument.Bookmarks(1).Range.Text
'Other Stuff
Exit Sub
Err_Handler:
ActiveDocument.Save
End Sub

I can work throug that issue using this construction. Do either of you
see problems with this method? Thanks.

Sub TestB()
On Error GoTo Err_Handler
MsgBox ActiveDocument.Bookmarks(1).Range.Text
'Other Stuff
Exit Sub
Err_ReEntry:
On Error Resume Next
ActiveDocument.Save
Exit Sub
Err_Handler:
Resume Err_ReEntry
End Sub




Greg Maxey wrote:
> Can someone help me understand why TestA works and TestB and TestC do not:
>
> Sub TestA()
> On Error Resume Next
> Err.Raise 6
> On Error GoTo SubErr:
> Err.Raise 13
> SubErr:
> MsgBox "Eureka!!"
> End Sub
>
> Sub TestB()
> On Error GoTo Err_Handler
> Err.Raise 6
> Err_Handler:
> On Error GoTo SubErr:
> Err.Raise 13
> SubErr:
> MsgBox "Eureka!!"
> End Sub
>
> Sub TestC()
> On Error GoTo Err_Handler
> Err.Raise 6
> Err_Handler:
> On Error Resume Next
> Err.Raise 13
> MsgBox "Eureka!!"
> End Sub
>
> In TestB and TestC, I have tried about every combination of GoTo, Resume, Err.Clear, that I can think of. I don't understand why the code isn't handling the second error. thanks.
> --
> Greg Maxey/Word MVP
> See:
> http://gregmaxey.mvps.org/word_tips.htm
> For some helpful tips using Word.
>
> ------=_NextPart_000_0026_01C7170E.4D45F5C0
> Content-Type: text/html; charset=iso-8859-1
> Content-Transfer-Encoding: quoted-printable
> X-Google-AttachSize: 1284
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML><HEAD>
> <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
> <META content="MSHTML 6.00.5730.11" name=GENERATOR>
> <STYLE></STYLE>
> </HEAD>
> <BODY bgColor=#f8f2d6>
> <DIV>Can someone help me understand why TestA works and TestB and TestC do
> not:</DIV>
> <DIV>&nbsp;</DIV>
> <DIV>Sub TestA()<BR>On Error Resume Next<BR>Err.Raise 6<BR>On Error GoTo
> SubErr:<BR>Err.Raise 13<BR>SubErr:<BR>MsgBox "Eureka!!"<BR>End Sub<BR></DIV>
> <DIV>Sub TestB()<BR>On Error GoTo Err_Handler<BR>Err.Raise
> 6<BR>Err_Handler:<BR>On Error GoTo SubErr:<BR>Err.Raise 13<BR>SubErr:<BR>MsgBox
> "Eureka!!"<BR>End Sub<BR></DIV>
> <DIV>Sub TestC()<BR>On Error GoTo Err_Handler<BR>Err.Raise
> 6<BR>Err_Handler:<BR>On Error Resume Next<BR>Err.Raise 13<BR>MsgBox
> "Eureka!!"<BR>End Sub<BR></DIV>
> <DIV>In TestB and TestC, I have tried about every combination of GoTo, Resume,
> Err.Clear, that I can think of.&nbsp; I don't understand why the code isn't
> handling the second error.&nbsp; thanks.<BR>-- <BR>Greg Maxey/Word
> MVP<BR>See:<BR><A
> href="http://gregmaxey.mvps.org/word_tips.htm">http://gregmaxey.mvps.org/word_tips.htm</A><BR>For
> some helpful tips using Word.<BR></DIV></BODY></HTML>
>
> ------=_NextPart_000_0026_01C7170E.4D45F5C0--


Re: Jay/Jezebel by Jay

Jay
Mon Dec 04 08:49:17 CST 2006

Greg Maxey wrote:
> Thanks Gents,
>
> I did reply last night, but see this morning that it didn't transmit.
> I have had a few error messages the past two days using OE that
> newsgroups coudn't be resolved?
>
> Anyway, thanks for both replies. Jezebel, by working I just meant the
> code would run without generating a runtime error. With your reply
> and Jay's I see now why it would and the others wouldn't. It was a
> poor example.
>
> The basic problem was this. I wanted an error handler that when an
> error was generated that it would simply ignore it an go on to save
> the document. I was working with TestA below which worked OK except
> when the user canceled or "X" out of the SaveAs Dialog. It would
> then throw and error while in the error handler.
>
> Sub TestA()
> On Error GoTo Err_Handler
> MsgBox ActiveDocument.Bookmarks(1).Range.Text
> 'Other Stuff
> Exit Sub
> Err_Handler:
> ActiveDocument.Save
> End Sub
>
> I can work throug that issue using this construction. Do either of
> you see problems with this method? Thanks.
>
> Sub TestB()
> On Error GoTo Err_Handler
> MsgBox ActiveDocument.Bookmarks(1).Range.Text
> 'Other Stuff
> Exit Sub
> Err_ReEntry:
> On Error Resume Next
> ActiveDocument.Save
> Exit Sub
> Err_Handler:
> Resume Err_ReEntry
> End Sub

Your TestB will work, and that style is common. But I personally find it
nonlinear and unintuitive, sending execution down to the bottom of the code
only to make it jump back to the point of the error. Besides that, if there
are multiple places in the routine that could trigger several different
kinds of errors, they all wind up going through Err_Handler and back to
wherever the error was. It can be a nightmare to debug, especially with
data-dependent errors.

I prefer a more linear and localized style, like this:

Sub TestJ()
On Error Resume Next
MsgBox ActiveDocument.Bookmarks(1).Range.Text
If Err.Number > 0 Then ' or just If Err.Number Then
Err.Clear
ActiveDocument.Save
Exit Sub
End If
'Other Stuff
End Sub

Each statement that could trigger an error can have a separate "error
handler" within an If Err.Number .. End If clause that immediately follows
that statement. You can nest these If clauses, one within another. If one
statement could trigger several kinds of errors, you can replace the simple
If clause with an If Err.Number = X .. ElseIf Err.Number = Y .. End If or a
Select Case Err.Number statement to do different things for different
errors. [If you program in other languages, you may recognize this style as
VBA's dumbed-down version of Try .. Catch blocks.]

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.



Re: Jay/Jezebel by Greg

Greg
Mon Dec 04 09:24:13 CST 2006

Thanks Jay,


Jay Freedman wrote:
> Greg Maxey wrote:
> > Thanks Gents,
> >
> > I did reply last night, but see this morning that it didn't transmit.
> > I have had a few error messages the past two days using OE that
> > newsgroups coudn't be resolved?
> >
> > Anyway, thanks for both replies. Jezebel, by working I just meant the
> > code would run without generating a runtime error. With your reply
> > and Jay's I see now why it would and the others wouldn't. It was a
> > poor example.
> >
> > The basic problem was this. I wanted an error handler that when an
> > error was generated that it would simply ignore it an go on to save
> > the document. I was working with TestA below which worked OK except
> > when the user canceled or "X" out of the SaveAs Dialog. It would
> > then throw and error while in the error handler.
> >
> > Sub TestA()
> > On Error GoTo Err_Handler
> > MsgBox ActiveDocument.Bookmarks(1).Range.Text
> > 'Other Stuff
> > Exit Sub
> > Err_Handler:
> > ActiveDocument.Save
> > End Sub
> >
> > I can work throug that issue using this construction. Do either of
> > you see problems with this method? Thanks.
> >
> > Sub TestB()
> > On Error GoTo Err_Handler
> > MsgBox ActiveDocument.Bookmarks(1).Range.Text
> > 'Other Stuff
> > Exit Sub
> > Err_ReEntry:
> > On Error Resume Next
> > ActiveDocument.Save
> > Exit Sub
> > Err_Handler:
> > Resume Err_ReEntry
> > End Sub
>
> Your TestB will work, and that style is common. But I personally find it
> nonlinear and unintuitive, sending execution down to the bottom of the code
> only to make it jump back to the point of the error. Besides that, if there
> are multiple places in the routine that could trigger several different
> kinds of errors, they all wind up going through Err_Handler and back to
> wherever the error was. It can be a nightmare to debug, especially with
> data-dependent errors.
>
> I prefer a more linear and localized style, like this:
>
> Sub TestJ()
> On Error Resume Next
> MsgBox ActiveDocument.Bookmarks(1).Range.Text
> If Err.Number > 0 Then ' or just If Err.Number Then
> Err.Clear
> ActiveDocument.Save
> Exit Sub
> End If
> 'Other Stuff
> End Sub
>
> Each statement that could trigger an error can have a separate "error
> handler" within an If Err.Number .. End If clause that immediately follows
> that statement. You can nest these If clauses, one within another. If one
> statement could trigger several kinds of errors, you can replace the simple
> If clause with an If Err.Number = X .. ElseIf Err.Number = Y .. End If or a
> Select Case Err.Number statement to do different things for different
> errors. [If you program in other languages, you may recognize this style as
> VBA's dumbed-down version of Try .. Catch blocks.]
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so
> all may benefit.