By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,148 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

Problem with err.raise

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
rkc

"Bogdan Zamfir" <bz*****@despammed.com> wrote in message
news:2n************@uni-berlin.de...
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?


The logic of what you are doing is correct, buit there are questions about
the
code you posted.

Do you have Option Explicit declared?
Where is MyVal declared and what does it have to do with anything? Maybe
it's
a typo and should be someVal.

The following code, a complete runnable version based on what you posted,
does exactly what you would expect it to do.

<clsErrTest>
Option Compare Database
Option Explicit

Dim cMyProp As String

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

</clsErrTest>

<test procedure>
Sub TestclsErrTest()

Dim myObj As clsErrTest
Set myObj = New clsErrTest
On Error GoTo Errhand

myObj.MyProp = "" ' this should raise the error
MsgBox Err.Description

exitHere:
If Not myObj Is Nothing Then
Set myObj = Nothing
End If
Exit Sub
Errhand:
MsgBox Err.Description & vbCrLf & _
"The code should (?) get here after assigning empty string to " & _
"MyProp, but it doesn't"
Resume exitHere

End Sub
</test procedure>

Nov 13 '05 #2

P: n/a
Hi,

Thank you for your reply
Yes, your code worked, and mine not, till I noticed a thing
About your questions, I typed that code as an example in the post, and not
copy from my real code, and indeed I have option explicit and MyVal should
be SomeVal

However, after I looked to your code and to mine (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

"rkc" <rk*@yabba.dabba.do.rochester.rr.bomb> wrote in message
news:cU*****************@twister.nyroc.rr.com...

"Bogdan Zamfir" <bz*****@despammed.com> wrote in message
news:2n************@uni-berlin.de...
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?


The logic of what you are doing is correct, buit there are questions about
the
code you posted.

Do you have Option Explicit declared?
Where is MyVal declared and what does it have to do with anything? Maybe
it's
a typo and should be someVal.

The following code, a complete runnable version based on what you posted,
does exactly what you would expect it to do.

<clsErrTest>
Option Compare Database
Option Explicit

Dim cMyProp As String

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

</clsErrTest>

<test procedure>
Sub TestclsErrTest()

Dim myObj As clsErrTest
Set myObj = New clsErrTest
On Error GoTo Errhand

myObj.MyProp = "" ' this should raise the error
MsgBox Err.Description

exitHere:
If Not myObj Is Nothing Then
Set myObj = Nothing
End If
Exit Sub
Errhand:
MsgBox Err.Description & vbCrLf & _
"The code should (?) get here after assigning empty string to " & _
"MyProp, but it doesn't"
Resume exitHere

End Sub
</test procedure>


Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.