473,379 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Throwing Argument Exceptions in Property Setters

Okay, silly question. Nitpicky, sure, but it bugs me.

Range-checking parameters in a property setter. For example, an Enum
property with one of two possible values:

Public Property MyEnumProperty() As MyEnumType
Get
Return m_myEnumProperty
End Get
Set(ByVal Value As MyEnumType)
If Value <MyEnumType.Value1 And Value <MyEnumType.Value2
Then
Throw New ArgumentOutOfRangeException("value")
End If
End Set
End Property

(Tacky code for illustrative purposes only!)

My question is this: When you guys are throwing an ArgumentException
in a property setter, do you specify "value" as the argument to the
exception, or do you use the name of the property?

It would *seem* that the correct answer is "value", the name of the
parameter. However, that's hardly helpful. You have to examine the
stack trace to determine which property threw the exception.

I am sorely tempted to use the property name to simplify defect
resolution. But I'm not sure that's a good idea.

On the other hand, I've considered a separate exception class that
indicates "Invalid property value" or something like that without
muddying the property name/parameter name waters.

So any advice you folks can provide would be appreciated.

Thanks in advance!

Apr 23 '07 #1
6 2835
I use the property name.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Mike Hofer" <kc********@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
Okay, silly question. Nitpicky, sure, but it bugs me.

Range-checking parameters in a property setter. For example, an Enum
property with one of two possible values:

Public Property MyEnumProperty() As MyEnumType
Get
Return m_myEnumProperty
End Get
Set(ByVal Value As MyEnumType)
If Value <MyEnumType.Value1 And Value <MyEnumType.Value2
Then
Throw New ArgumentOutOfRangeException("value")
End If
End Set
End Property

(Tacky code for illustrative purposes only!)

My question is this: When you guys are throwing an ArgumentException
in a property setter, do you specify "value" as the argument to the
exception, or do you use the name of the property?

It would *seem* that the correct answer is "value", the name of the
parameter. However, that's hardly helpful. You have to examine the
stack trace to determine which property threw the exception.

I am sorely tempted to use the property name to simplify defect
resolution. But I'm not sure that's a good idea.

On the other hand, I've considered a separate exception class that
indicates "Invalid property value" or something like that without
muddying the property name/parameter name waters.

So any advice you folks can provide would be appreciated.

Thanks in advance!

Apr 23 '07 #2
Hello,

If I recall the Framework Design Guidelines correctly, you should not throw
an argument in a property setter. Rather throw an exception when the
property is used in member method.

Best regards,
Henning Krause

"Mike Hofer" <kc********@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
Okay, silly question. Nitpicky, sure, but it bugs me.

Range-checking parameters in a property setter. For example, an Enum
property with one of two possible values:

Public Property MyEnumProperty() As MyEnumType
Get
Return m_myEnumProperty
End Get
Set(ByVal Value As MyEnumType)
If Value <MyEnumType.Value1 And Value <MyEnumType.Value2
Then
Throw New ArgumentOutOfRangeException("value")
End If
End Set
End Property

(Tacky code for illustrative purposes only!)

My question is this: When you guys are throwing an ArgumentException
in a property setter, do you specify "value" as the argument to the
exception, or do you use the name of the property?

It would *seem* that the correct answer is "value", the name of the
parameter. However, that's hardly helpful. You have to examine the
stack trace to determine which property threw the exception.

I am sorely tempted to use the property name to simplify defect
resolution. But I'm not sure that's a good idea.

On the other hand, I've considered a separate exception class that
indicates "Invalid property value" or something like that without
muddying the property name/parameter name waters.

So any advice you folks can provide would be appreciated.

Thanks in advance!
Apr 23 '07 #3
Henning Krause [MVP - Exchange] <ne***************@this.infinitec.de>
wrote:
If I recall the Framework Design Guidelines correctly, you should not throw
an argument in a property setter. Rather throw an exception when the
property is used in member method.
No way - part of what properties are there for is to allow validation
to be encapsulated.

See http://msdn2.microsoft.com/en-us/lib...06(VS.80).aspx

<quote>
It is valid and acceptable to throw exceptions from a property setter.
</quote>

However, the same page *does* say that it's not a good idea to throw
exceptions from a getter. Might that be what you're thinking of?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 23 '07 #4
Hello Jon,
>
However, the same page *does* say that it's not a good idea to throw
exceptions from a getter. Might that be what you're thinking of?
Yes, that's right. Just looked it up in the book.

Best regards,
Henning Krause
Apr 24 '07 #5
In fact, I confused it with the statement

"Do allow properties to be set in any order even if this results in a
temporary invalid state of the object."

In this situation, exceptions should deferred to the point where they are
used together.

Best regards,
Henning Krause

"Henning Krause [MVP - Exchange]" <ne***************@this.infinitec.de>
wrote in message news:e2**************@TK2MSFTNGP02.phx.gbl...
Hello Jon,
>>
However, the same page *does* say that it's not a good idea to throw
exceptions from a getter. Might that be what you're thinking of?

Yes, that's right. Just looked it up in the book.

Best regards,
Henning Krause
Apr 24 '07 #6
Henning Krause [MVP - Exchange] <ne***************@this.infinitec.de>
wrote:
In fact, I confused it with the statement

"Do allow properties to be set in any order even if this results in a
temporary invalid state of the object."

In this situation, exceptions should deferred to the point where they are
used together.
Personally, I try to avoid using properties for that kind of thing -
I'd rather have a setter method which took both values and validated
them against each other. Either that or have a separate
"initialisation/validation" step which means "I've finished setting
properties now - validate them and don't let me change them again".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 24 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
15
by: Sek | last post by:
Gurus, I am wondering whether it is right to throw an exception from a Property of an object. To get into it further, is it okay to throw exception during 'get' operation? I was searching...
40
by: Sek | last post by:
Is it appropriate to throw exception from a constructor? Thats the only way i could think of to denote the failure of constructor, wherein i am invoking couple of other classes to initialise the...
7
by: Sek | last post by:
Hi Folks! I was pondering over a code and noticed that exception handlers were present in the private, protected as well as public methods. And, ofcourse, public methods were calling priv/prot...
3
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own...
7
by: jm.suresh | last post by:
Hi, In the following program, I have a class Test which has a property x. Its setx function gets a string value and converts it into a float and stores into it. class Test(object): def...
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
3
by: Mike Hofer | last post by:
Quick question. When you guys throw an ArgumentException (or one of its deriviatives) from a property setter, what do you use as the argument to the exception's constructor? For instance: ...
1
by: mk | last post by:
It seems like getter is defined in such way that it passes only 'self': class FunDict(dict): def __init__(self): self.fundict = dict() def fget(self, fun): return fundict
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.