Home All Groups Group Topic Archive Search About

Error handling is handling me

Author
10 May 2005 8:00 PM
Chris Lieb
I'm trying to get used to VB6's error handling and have run into a problem. 
I have a sub, Add, in a module, modBase, that calls a sub, myAdd, in a form,
frmSLIC_report.  If the data that is checked has an error, I want both myAdd
and Add to exit immediately.  I figured that I would raise an error in myAdd
using Err.Raise.  I would not handle it there, allowing the error to then
move up to the Add method, where I have an error handler that exits the
function.  However, when I cause the error, I get the dialog for it while I
am still in the myAdd method.  The error never makes it to the handler for it
in Add.  Here's a bit of code to help clarify what I just tried to say:

modBase:

Public Sub Add(ByRef theForm As Form, table As String, pk As String)
    On Error GoTo oops
    Dim exText as clsField
    Set exText = theForm.myAdd(aControl)
        ...
    If False Then
oops: Exit Sub
    EndIf
End Sub

frmSLIC_report

Public Function myAdd(ByRef item As Control) As clsField
    Set myAdd = New cslField
    If item.name = "txtKeyword" And item.Text = "" Then
        MsgBox "Please enter a keyword for this record", , "Error"
        Err.Raise 2005, "myAdd(txtKeyword)", "You must enter a keyword for
every record"
    End If
End Function

This is by no means the actual code, just a good example of what I am doing.
I am getting the run-tim error while in the myAdd function instead of the
Add method where I would expect it.  How can I make the error go to the right
handler?  In .NET, you could just put a throws clause in the declaration, but
AFAIK you can't do anything like that in VB6.

Thanks in advance
--
Chris Lieb
UPS CACH, Hodgekins, IL
Tech Support Group - Systems/Apps

Author
10 May 2005 8:30 PM
Ken Halter
"Chris Lieb" <ChrisL***@discussions.microsoft.com> wrote in message
news:210F1C62-3F54-4215-8CBA-DED419C08015@microsoft.com...
> I'm trying to get used to VB6's error handling and have run into a
> problem.

First of all, make sure your error handling settings are "correct".
Tools/Options/General - Break on Unhandled Errors.

This code.... I've never seen code that jumps into the middle of an If/End
If block like that so I actually tried it and VB didn't complain.... very
wierd <g>

>    If False Then
> oops: Exit Sub
>    EndIf


--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Sign up now to help keep VB support alive - http://classicvb.org/petition
Please keep all discussions in the groups..
Author
10 May 2005 9:46 PM
Chris Lieb
You were right, I had it set to break in class module instead of on unhandled
errors.  As for my error handling code, I know that it looks weird, but by
putting it in an If block that is always false, I know that it will never get
executed unless I GoTo the label.  I also don't like peppering Exit and Exit
Sub and Exit Function calls all around my methods unless they are there to
decrease execute time (ie, exit a loop if a certain condition is met).

Show quoteHide quote
"Ken Halter" wrote:

> "Chris Lieb" <ChrisL***@discussions.microsoft.com> wrote in message
> news:210F1C62-3F54-4215-8CBA-DED419C08015@microsoft.com...
> > I'm trying to get used to VB6's error handling and have run into a
> > problem.
>
> First of all, make sure your error handling settings are "correct".
> Tools/Options/General - Break on Unhandled Errors.
>
> This code.... I've never seen code that jumps into the middle of an If/End
> If block like that so I actually tried it and VB didn't complain.... very
> wierd <g>
>
> >    If False Then
> > oops: Exit Sub
> >    EndIf
>
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> Sign up now to help keep VB support alive - http://classicvb.org/petition
> Please keep all discussions in the groups..
>
>
>
Author
11 May 2005 7:46 PM
Charlie
Show quote Hide quote
"Chris Lieb" wrote:

> ...As for my error handling code, I know that it looks weird, but by
> putting it in an If block that is always false, I know that it will never get
> executed unless I GoTo the label.  I also don't like peppering Exit and Exit
> Sub and Exit Function calls all around my methods unless they are there to
> decrease execute time (ie, exit a loop if a certain condition is met).
>
> Public Sub Add(ByRef theForm As Form, table As String, pk As String)
>    On Error GoTo oops
>    Dim exText as clsField
>    Set exText = theForm.myAdd(aControl)
>        ...
>    If False Then
>oops: Exit Sub
>    EndIf
>End Sub

It can be done but it is not a good coding practice to jump into the middle
of a block of code like that.  Especially for the reasons you give.  For one,
if all you want to do is simply exit the sub without doing anything, you can
put the label before the End Sub statement.

oops:
End Sub

A label is not an executable statement -- it is just an address to which
program control will jump, on error in this case.  The address used is
actually the line after the label.  The compiler removes the label and
control jumps to the End Sub statement, (which does an Exit Sub, BTW).

Since the label is not really an executable statement it does not slow down
your program when no error occurs.  On the other hand the If statement that
you use...

    If False Then
oops: Exit Sub
    EndIf

....WILL be executed when no error occurs, (unless VB has a very good
optimizing compiler to see through that trick).  So adding that If block is
not only extra coding work for you, it is contrary to what you are trying to
do!

P.S. I like your choice of label name (oops)!
Author
10 May 2005 8:33 PM
Jack
Chris,

Error handling is very limiting in VB.  I use HuntERR which is a shareware
tool.  It takes a little time to implement but it is worth it.  You can
reraise errors that you want to and then you can not reraise the errors.

Good Luck

Show quoteHide quote
"Chris Lieb" <ChrisL***@discussions.microsoft.com> wrote in message
news:210F1C62-3F54-4215-8CBA-DED419C08015@microsoft.com...
> I'm trying to get used to VB6's error handling and have run into a
> problem.
> I have a sub, Add, in a module, modBase, that calls a sub, myAdd, in a
> form,
> frmSLIC_report.  If the data that is checked has an error, I want both
> myAdd
> and Add to exit immediately.  I figured that I would raise an error in
> myAdd
> using Err.Raise.  I would not handle it there, allowing the error to then
> move up to the Add method, where I have an error handler that exits the
> function.  However, when I cause the error, I get the dialog for it while
> I
> am still in the myAdd method.  The error never makes it to the handler for
> it
> in Add.  Here's a bit of code to help clarify what I just tried to say:
>
> modBase:
>
> Public Sub Add(ByRef theForm As Form, table As String, pk As String)
>    On Error GoTo oops
>    Dim exText as clsField
>    Set exText = theForm.myAdd(aControl)
>        ...
>    If False Then
> oops: Exit Sub
>    EndIf
> End Sub
>
> frmSLIC_report
>
> Public Function myAdd(ByRef item As Control) As clsField
>    Set myAdd = New cslField
>    If item.name = "txtKeyword" And item.Text = "" Then
>        MsgBox "Please enter a keyword for this record", , "Error"
>        Err.Raise 2005, "myAdd(txtKeyword)", "You must enter a keyword for
> every record"
>    End If
> End Function
>
> This is by no means the actual code, just a good example of what I am
> doing.
> I am getting the run-tim error while in the myAdd function instead of the
> Add method where I would expect it.  How can I make the error go to the
> right
> handler?  In .NET, you could just put a throws clause in the declaration,
> but
> AFAIK you can't do anything like that in VB6.
>
> Thanks in advance
> --
> Chris Lieb
> UPS CACH, Hodgekins, IL
> Tech Support Group - Systems/Apps
Author
10 May 2005 9:04 PM
Ken Halter
"Jack" <j***@email.com> wrote in message
news:ePIH8AaVFHA.2328@TK2MSFTNGP10.phx.gbl...
> Chris,
>
> Error handling is very limiting in VB.  I use HuntERR which is a shareware
> tool.  It takes a little time to implement but it is worth it.  You can
> reraise errors that you want to and then you can not reraise the errors.
>
> Good Luck

hmmm... I just checked out HuntERR. Unless I'm missing something, it's free
w/source. Since I didn't try to actually "use" it, that may be where the
"shareware" aspect pops up...

Error-Handling Solution for Visual Basic
http://www.urfinjus.net/UJv3/prodvb6/hunterr.aspx

I may play with this a while......

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Sign up now to help keep VB support alive - http://classicvb.org/petition
Please keep all discussions in the groups..
Author
10 May 2005 9:10 PM
Jack
Ken,

I made a type mistake.  It is freeware not shareware.

Sorry

Show quoteHide quote
"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:%23sGYQPaVFHA.4056@TK2MSFTNGP15.phx.gbl...
> "Jack" <j***@email.com> wrote in message
> news:ePIH8AaVFHA.2328@TK2MSFTNGP10.phx.gbl...
>> Chris,
>>
>> Error handling is very limiting in VB.  I use HuntERR which is a
>> shareware tool.  It takes a little time to implement but it is worth it.
>> You can reraise errors that you want to and then you can not reraise the
>> errors.
>>
>> Good Luck
>
> hmmm... I just checked out HuntERR. Unless I'm missing something, it's
> free w/source. Since I didn't try to actually "use" it, that may be where
> the "shareware" aspect pops up...
>
> Error-Handling Solution for Visual Basic
> http://www.urfinjus.net/UJv3/prodvb6/hunterr.aspx
>
> I may play with this a while......
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> Sign up now to help keep VB support alive - http://classicvb.org/petition
> Please keep all discussions in the groups..
>