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 2 5414
"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>
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>
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Remy C. Cool |
last post by:
Hello,
My application uses a remote import scheme using the sys.path_hooks
solution as explained in PEP302
(http://www.python.org/peps/pep-0302.html)
The importer works fine for packages,...
|
by: Chris S. |
last post by:
A wrote a small class to handle IO through pipes, but the connection
only seems to work in one direction. The following code defines
connection.py, engine.py, and controller.py. Once connected, the...
|
by: steven |
last post by:
Hi,
Anyone was using pmock for unit testing with python? I met a problem
and hope someone to help me. For short, the pmock seems can not mock a
iterator object.
For example, the tested...
|
by: virgo81 |
last post by:
hi~
I made smartclient.
First, I made a StrongNamed Key(snk file).
I add my key file and set allow partiallytrustedcllers.
|
by: Ralph Soons |
last post by:
Hi all,
I am trying to save the viewstate in a session instead of storing it in a
hidden of the webpage which is default. This because of performance reasons.
When I use line 2 in combination...
|
by: francois |
last post by:
Hi guys,
I have quite big troublesome performance problem.
then i did a very simpkle page with a dropdownlist, when u click on it it
makes a simple sql select on the SQL server and then...
|
by: Wei Wang |
last post by:
I get compile error for this code:
for i in 1..arg_count-1 LOOP
RAISE NOTICE quote_literal(to_char(i, ''9''));
END LOOP;
where arg_count = 3.
I tried RAISE NOTICE to_char(i, ''9''); as...
|
by: Dmitry Prokoptsev |
last post by:
Hello,
I need to write a class for exceptions which can be thrown, caught,
stored and thrown once again. I have written the following code:
--- begin ---
#include <string>
class Exception...
|
by: Martin |
last post by:
Hi all,
I'm having a problem when trying to raise an event in my custom control when
a toolstripbutton enable state changes. The problem is that although the
code to raise the event executes, my...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |