2013-02-27 104 views
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值。但不知何故,這似乎並不是正確的做法。

回答

3

Catching and Throwing Standard Exception Types的例子在MSDN你應該保持設置「值」作爲參數名:

做使用價值財產 制定者的隱含值參數的名稱。

所以沒關係:

Throw New ArgumentNullException("value", "value cannot be empty") 
+0

不錯!不知何故,該網頁沒有出現在我的Google搜索結果中。謝謝。 – 2013-02-27 21:57:06