2
从MSDN使用什么参数名的ArgumentException:在属性setter
每一个ArgumentException的应随身携带导致该异常的参数的名称。
我的问题:如果一个属性setter方法应该抛出一个ArgumentException
,我应该给它的setter的参数名称(默认:value
)或属性的名称?
例子:
Private _myProperty As String
Public Property MyProperty As String
Get
Return _myProperty
End Get
Set(value As String)
If String.IsNullOrEmpty(value) Then
' what I've been doing for the last 2 years
Throw New ArgumentNullException("value", "value cannot be empty")
' what I think I should be doing instead
Throw New ArgumentNullException("MyProperty", "value cannot be empty")
End If
_myProperty = value
End Set
End Property
我希望是有道理的。你怎么看?
编辑
我想另一个解决办法是重新命名value
为更有意义的,然后使用该作为paramName
值。但不知何故,这似乎并不是正确的做法。
不错!不知何故,该网页没有出现在我的Google搜索结果中。谢谢。 – 2013-02-27 21:57:06