Problem with err.raise

  • Thread starter Thread starter Bogdan Zamfir
  • Start date Start date
B

Bogdan Zamfir

Hi,

I have a problem with err.raise
I write a class module, and I want to use this mechanism to indicate errors
when user set wrong values to properties
But it seems the error is never raised (although, when I exit from the
property let code, if I check err.number and message, I get the error
message I set in err.raise statement

But my calling code never throw me into error handler if I set wrong
property value

To be more specific:

This is the code in my class

dim cMyProp as string

property Let MyProp (byval SomeVal as string)
if MyVal ="" then
err.raise Err.Raise vbObjectError + 1000, "Let property MyProp",
"Property does not accept empty string"
else
cMyProp = Someval
endif
end property

And in another module I have this code

.....
on error goto Errhand

TestLine: myObj.MyProp = "" ' this should raise the error
debug.print err.description
exit sub

Errhand:
MsgBox "The code should (?) get here after assigning empty string to
MyProp, but it doesn't"

....

So when I execute TestLine, from my understanding, the prop should raise the
error and my code should jump to err handler
But this isn't happening
Although, if I test myself err.number and message exactly after TestLine, it
contains the error I try to raise.

How to handle this? Should I test everytime the err.number and
err.description in such cases?

Thank you
 
Hi,
This works fine for me.
Here is the code in my class (clsTest):

Property Let MyProp(ByVal SomeVal As String)
If SomeVal = "" Then
err.Raise vbObjectError + 1000, "Let property MyProp", "Property does not accept empty string"
Else
cMyProp = SomeVal
End If
End Property

Here is how I test it:
Private Sub cmdClass_Click()
Dim myClass As clsTest
On Error GoTo c_err
Set myClass = New clsTest

myClass.MyProp = ""
Exit Sub
c_err:
MsgBox err.Description
End Sub

For me, the message box comes up with the correct error message. Are you saying for you it simply ignores the error entirely?
 
Hi,

Yes, your code worked, and mine not, till I noticed a thing

However, after I looked again to my code (the real one), I noticed I
have a on error resume next few lines above the raise statement
And I didn't reset it with other on error statement. Actually, my issue was
the Someval should be a printer name, and I use it to get a reference to a
valid printer object. And I use on error resume next just before the code

on error resume next
set oMyPrinter = printers(SomeVal)
if oMyPrinter is nothing then
...(bla, bla)

And it seems this on error resume next was the problem,

But as I know, when I left the sub with on error resume next statement, the
error handling should be automatically restored to setting from calling sub,
isn't it?
But in my case it didn't restored, and this caused the error not to be
raised.

Anyway, I found and fixed this, and thank you for your post.

Cheers
Bogdan

Dan Artuso said:
Hi,
This works fine for me.
Here is the code in my class (clsTest):

Property Let MyProp(ByVal SomeVal As String)
If SomeVal = "" Then
err.Raise vbObjectError + 1000, "Let property MyProp", "Property does not accept empty string"
Else
cMyProp = SomeVal
End If
End Property

Here is how I test it:
Private Sub cmdClass_Click()
Dim myClass As clsTest
On Error GoTo c_err
Set myClass = New clsTest

myClass.MyProp = ""
Exit Sub
c_err:
MsgBox err.Description
End Sub

For me, the message box comes up with the correct error message. Are you
saying for you it simply ignores the error entirely?
 
Back
Top